We use series arithmetic and alignment to do math on data that has labels. It helps us add, subtract, or compare data even if the labels don't match exactly.
Series arithmetic and alignment in Data Analysis Python
result = series1 + series2 result = series1 - series2 result = series1 * series2 result = series1 / series2
Series are like lists with labels (indexes).
When you do arithmetic, pandas matches data by these labels automatically.
import pandas as pd s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c']) s2 = pd.Series([1, 2, 3], index=['a', 'b', 'd']) result = s1 + s2 print(result)
s1 = pd.Series([5, 10, 15], index=['x', 'y', 'z']) s2 = pd.Series([2, 4, 6], index=['x', 'y', 'z']) result = s1 * s2 print(result)
s1 = pd.Series([100, 200], index=['p', 'q']) s2 = pd.Series([50], index=['p']) result = s1 - s2 print(result)
This program adds sales numbers from January and February. It shows how pandas adds values with matching labels and puts NaN where a label is missing in one series.
import pandas as pd # Create two series with some overlapping and some unique labels sales_jan = pd.Series([250, 150, 400], index=['apples', 'bananas', 'oranges']) sales_feb = pd.Series([200, 180, 100, 300], index=['apples', 'bananas', 'pears', 'oranges']) # Add sales from January and February combined_sales = sales_jan + sales_feb print(combined_sales)
If a label is missing in one series, the result for that label is NaN (not a number).
You can use methods like .fillna() to replace NaN with a number if needed.
Arithmetic operations align data by labels automatically, no need to sort or match manually.
Series arithmetic matches data by labels before doing math.
Results have NaN where labels don't match.
This makes combining and comparing labeled data easy and safe.
Practice
+ operator?Solution
Step 1: Understand pandas Series label alignment
When adding two Series, pandas aligns data by their labels (index) before performing arithmetic.Step 2: Effect of label mismatch on addition
If a label is missing in one Series, the result for that label is NaN because pandas cannot add missing data.Final Answer:
Values are added only where labels match; others become NaN -> Option DQuick Check:
Series addition aligns labels = Values are added only where labels match; others become NaN [OK]
- Assuming addition ignores labels and adds by position
- Expecting an error on label mismatch
- Thinking missing labels fill with zero automatically
s1 and s2 with label alignment, allowing specification of fill_value for missing labels?Solution
Step 1: Check syntax for adding Series with alignment and fill_value
Usings1.add(s2)adds Series with label alignment and allowsfill_valuefor missing labels.Step 2: Compare other options
s1 + s2adds with alignment but nofill_valueoption.s1 + s2.valuesignores labels (positional).s1.append(s2)concatenates.Final Answer:
s1.add(s2) -> Option CQuick Check:
Use.add()for aligned addition withfill_value[OK]
.add() method for safe label-aligned addition [OK]- Using
+with raw values ignoring labels - Confusing append with addition
- Not knowing
.add()exists for Series
import pandas as pd s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c']) s2 = pd.Series([1, 3, 3], index=['b', 'c', 'd']) result = s1 + s2 print(result)
What is the output?
Solution
Step 1: Align Series by labels
s1 has 'a':10, 'b':20, 'c':30; s2 has 'b':1, 'c':3, 'd':3. Union index: 'a','b','c','d'.Step 2: Add values where labels match, NaN otherwise
'a': 10 + NaN = NaN, 'b':20+1=21.0, 'c':30+3=33.0, 'd': NaN + 3 = NaN. Floats due to NaN.Final Answer:
a NaN b 21.0 c 33.0 d NaN -> Option AQuick Check:
Series addition aligns labels, unmatched get NaN [OK]
- Adding values by position ignoring labels
- Expecting no NaN for unmatched labels
- Misreading index labels and values
import pandas as pd s1 = pd.Series([5, 10], index=['x', 'y']) s2 = pd.Series([2, 4, 6], index=['x', 'y', 'z']) result = s1 + s2 print(result)
The result contains NaN. What is the problem and how to fix it to treat missing values as zero?
Solution
Step 1: Check the code execution
The code runs but produces NaN for 'z' (missing in s1). Alignment by labels causes NaN for unmatched.Step 2: Fix with fill_value
s1.add(s2, fill_value=0)fills missing labels with 0: 'x':7, 'y':14, 'z':6.Final Answer:
NaN due to different lengths; fix by using s1.add(s2, fill_value=0) -> Option BQuick Check:
Use.add()with fill_value to avoid NaN [OK]
.add(fill_value=0) to handle missing labels safely [OK]- Thinking
+raises error on length mismatch - Using concat instead of arithmetic
- Ignoring missing labels causing NaN
store1 = pd.Series([100, 200, 300], index=['apple', 'banana', 'orange']) store2 = pd.Series([50, 150, 100], index=['banana', 'orange', 'grape'])
How do you create a Series showing total sales per fruit, treating missing fruits as zero?
Solution
Step 1: Understand the problem
We want to add sales per fruit, but some fruits are missing in one store's Series.Step 2: Use addition with fill_value=0
Usingstore1.add(store2, fill_value=0)adds values label-wise, treating missing labels as zero, so no NaN appears.Final Answer:
store1.add(store2, fill_value=0) -> Option AQuick Check:
Add Series with fill_value=0 to handle missing labels [OK]
.add(fill_value=0) to sum with missing labels as zero [OK]- Using
+causing NaN for missing labels - Using append and sum incorrectly
- Using combine without fill_value causing NaN
Ready to prove it?
Pass the test and this pattern moves unknown
