← Back to Pandas Course | Chapter 2: Series | Lesson 4 of 7

Series operations

Arithmetic on Series lines up the labels first, so values are matched by index, not by position.

In this page:

  1. Series operations
Syntax
python
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

python
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
  1. Assuming addition is positional
  2. Forgetting NaN appears for unmatched labels
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.