0PricingLogin
Pandas & NumPy Academy · Lesson

Vectorized Operations on Series

Perform arithmetic between two Series, handle index alignment automatically, and fill missing aligned values with NaN.

Why Vectorized Operations?

Like NumPy, Pandas Series support vectorized operations that apply arithmetic, comparisons, and functions to every element without Python loops. This provides both conciseness and speed. Under the hood each operation delegates to NumPy's C-level routines, but Pandas adds a critical bonus: automatic index alignment before performing the operation.

import pandas as pd

revenue = pd.Series([1000, 2000, 1500], index=['Jan', 'Feb', 'Mar'])
tax_rate = 0.2
net = revenue * (1 - tax_rate)   # vectorized -- no loop
print(net)

Arithmetic Between Two Series

When you add, subtract, multiply, or divide two Series, Pandas aligns them by index label first. Only positions where both Series have a label produce a result; unmatched labels produce NaN. This behaviour prevents hidden bugs from mis-aligned data — for example, adding January figures from two data sources that happen to be stored in different orders.

import pandas as pd

a = pd.Series([10, 20, 30], index=['x', 'y', 'z'])
b = pd.Series([1, 2, 3], index=['y', 'z', 'w'])

print(a + b)
# w     NaN
# x     NaN
# y    21.0
# z    32.0

All lessons in this course

  1. Creating a Series
  2. Label-Based and Position-Based Access
  3. Vectorized Operations on Series
  4. Useful Series Methods
← Back to Pandas & NumPy Academy