Skip to content
Activities · Activity 2.6

Q.Draw two tables for division similar to tables 2.2 and 2.3 showing the changes in the series elements and corresponding output without replacing the missing values, and after replacing the missing values with 0.
[Table: In the textbook, Tables 2.2 and 2.3 tabulate the addition of seriesA = pd.Series([1,2,3,4,5], index=['a','b','c','d','e']) and seriesB = pd.Series([10,20,-10,-50,100], index=['z','y','a','c','e']). Table 2.2 (seriesA + seriesB, without replacing missing values) lists, per index, the value from seriesA, the value from seriesB, and the sum — a: 1, -10, -9.0; b: 2, (missing), NaN; c: 3, -50, -47.0; d: 4, (missing), NaN; e: 5, 100, 105.0; y: (missing), 20, NaN; z: (missing), 10, NaN. Table 2.3 (seriesA.add(seriesB, fill_value=0), missing values replaced with 0) lists — a: 1, -10, -9.0; b: 2, 0, 2.0; c: 3, -50, -47.0; d: 4, 0, 4.0; e: 5, 100, 105.0; y: 0, 20, 20.0; z: 0, 10, 10.0.]

Puducherry CbseNCERTSubjective· 4mImportance★★★★★
16% · 6/38 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This solution demonstrates how Pandas Series handle missing values during division, first by default (resulting in NaN) and then by explicitly replacing missing values with 0 using the fill_value parameter, which can lead to inf (infinity) or 0.0 results.

When performing arithmetic operations between two Pandas Series, the library automatically aligns the data based on their indices. If an index exists in one Series but not the other, Pandas treats the value for that missing index as NaN (Not a Number) by default. Any arithmetic operation involving NaN will propagate NaN to the result.

To control how these missing values are handled, Pandas Series arithmetic methods (like add(), sub(), mul(), div()) provide a fill_value parameter. This parameter allows you to specify a value that should be used in place of NaN for any index that is present in one Series but not the other, before the arithmetic operation is performed. This is particularly useful when you want to treat missing data as a specific numerical value, such as 0.

Let's define the two Series as given:

import pandas as pd

seriesA = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])
seriesB = pd.Series([10, 20, -10, -50, 100], index=['z', 'y', 'a', 'c', 'e'])

print("seriesA:")
print(seriesA)
print("\nseriesB:")
print(seriesB)
seriesA:
a    1
b    2
c    3
d    4
e    5
dtype: int64

seriesB:
z     10
y     20
a    -10
c    -50
e    100
dtype: int64

Division without replacing missing values

When you perform division using the / operator or the div() method without specifying fill_value, Pandas aligns the Series by index. For any index present in only one of the Series, the corresponding value from the other Series is considered NaN. The division operation then proceeds, and since any arithmetic operation with NaN results in NaN, these mismatched indices will yield NaN in the output.

# Division without replacing missing values
result_no_fill = seriesA / seriesB
print("\nResult of seriesA / seriesB (without fill_value):")
print(result_no_fill)
a   -0.10
b     NaN
c   -0.06
d     NaN
e    0.05
y     NaN
z     NaN
dtype: float64

The table below shows the values from seriesA and seriesB for each unique index, and the resulting output when no fill_value is specified. Notice how indices 'b', 'd' (only in seriesA) and 'y', 'z' (only in seriesB) lead to NaN in the output.

IndexValue from seriesAValue from seriesBOutput (seriesA / seriesB)
a1-10-0.1
b2(missing)NaN
c3-50-0.06
d4(missing)NaN
e51000.05
y(missing)20NaN
z(missing)10NaN

Division after replacing missing values with 0

Using the fill_value=0 argument with the div() method tells Pandas to substitute 0 for any missing values before performing the division.

# Division after replacing missing values with 0
result_fill_zero = seriesA.div(seriesB, fill_value=0)
print("\nResult of seriesA.div(seriesB, fill_value=0):")
print(result_fill_zero)
a   -0.10
b     inf
c   -0.06
d     inf
e    0.05
y    0.00
z    0.00
dtype: float64
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.