Skip to content
Activities · Activity 2.4

Q.Draw two tables for subtraction 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 1000.
[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.]

West Bengal WbchseTextbookSubjective· 4mImportance★★★★★
11% · 4/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 →

Construct two tables showing element-wise subtraction of the two Series: one with default NaN propagation, the other using fill_value=1000 to replace missing values before subtraction.

Why Missing Value Handling Matters in Series Arithmetic

When you perform arithmetic between two Series with non-identical indices, pandas aligns them by index label. Any label present in one Series but absent in the other produces a missing value (NaN) at that position. By default, any operation involving NaN yields NaN in the result.

The .sub() method (and its siblings .add(), .mul(), etc.) accepts a fill_value parameter. This parameter does not replace existing NaN entries in your data; instead, it substitutes a concrete value for positions where one Series has a valid entry and the other has no entry at all due to index mismatch. Think of it as saying "if a label exists in only one Series, treat the missing partner as this fill value rather than NaN."

For subtraction the operation is seriesA - seriesB, so the table columns will show the value from seriesA, the value from seriesB, and seriesA - seriesB.


Table 1: Subtraction Without Replacing Missing Values

This is the result of seriesA - seriesB (or equivalently seriesA.sub(seriesB)). Where an index exists in only one Series, the corresponding value in the other is treated as NaN, and the result is NaN.

IndexseriesAseriesBseriesA − seriesB
a1−1011.0
b2(missing)NaN
c3−5053.0
d4(missing)NaN
e5100−95.0
y(missing)20NaN
z(missing)10NaN
Watch out

Indices b, d, y, and z all produce NaN because one Series has no value at that label. The arithmetic cannot proceed without a number on both sides.


Table 2: Subtraction After Replacing Missing Values with 1000

This is the result of seriesA.sub(seriesB, fill_value=1000). Wherever an index appears in only one Series, the missing partner is treated as 1000 before subtraction.

IndexseriesAseriesBseriesA − seriesB
a1−1011.0
b21000−998.0
c3−5053.0
d41000−996.0
e5100−95.0
y100020980.0
z100010990.0

Key observations:

  • Index b: seriesA has 2, seriesB is missing → treated as 2 − 1000 = −998.0.
  • Index d: seriesA has 4, seriesB is missing → 4 − 1000 = −996.0.
  • Index y: seriesA is missing → treated as 1000, seriesB has 20 → 1000 − 20 = 980.0.
  • Index z: seriesA is missing → treated as 1000, seriesB has 10 → 1000 − 10 = 990.0.
  • Indices a, c, e: both Series have values, so fill_value is irrelevant; the subtraction proceeds normally. …

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.