vectorization vs loops
Vectorized operations run in fast compiled code, while Python loops over rows are far slower.
In this page:
Syntax
df["result"] = df["column"] * 2
df["result"] = np.where(condition, value1, value2)
vectorization vs loops
Whole-column operations such as df["a"] * 2 or np.where avoid the overhead of Python loops. iterrows and manual loops are typically 10 to 100 times slower. Reach for built-ins first, then apply, and loops last.
Note:
If you find yourself writing iterrows, look for a vectorized alternative.
Example: vectorization vs loops
import numpy as np
import pandas as pd
df = pd.DataFrame({"a": range(1, 6)})
looped = [x * 2 for x in df["a"]]
vector = df["a"] * 2
df["size"] = np.where(df["a"] > 3, "big", "small")
print(looped == vector.tolist())
print(df)
# Output:
# True
# a size
# 0 1 small
# 1 2 small
# 2 3 small
# 3 4 big
# 4 5 big
Related Topics
Common Mistakes
- Using iterrows for calculations
- Growing DataFrames row by row
- Using apply where a vectorized method exists
Chapter Summary
- Vectorize whole columns
- Loops are far slower
- np.where replaces if/else
- Use loops as a last resort
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: