Series operations
Arithmetic on Series lines up the labels first, so values are matched by index, not by position.
In this page:
Syntax
s1 + s2
s * scalar
s.sum()
Series operations
Operators work element-wise and align on the index. Labels present in only one Series give NaN in the result. Use methods like add with fill_value to control that behaviour.
Note:
s1.add(s2, fill_value=0) treats missing labels as 0.
Example: Series operations
import pandas as pd
a = pd.Series([1, 2, 3], index=["x", "y", "z"])
b = pd.Series([10, 20, 30], index=["y", "z", "w"])
print(a + b)
print(a.add(b, fill_value=0))
# Output:
# w NaN
# x NaN
# y 12.0
# z 23.0
# dtype: float64
# w 30.0
# x 1.0
# y 12.0
# z 23.0
# dtype: float64
Related Topics
Common Mistakes
- Assuming addition is positional
- Forgetting NaN appears for unmatched labels
- Dropping labels by accident
Chapter Summary
- Arithmetic aligns on the index
- Unmatched labels become NaN
- add with fill_value fills gaps
- Scalars broadcast
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: