← Back to Pandas Course | Chapter 13: Performance & Best Practices | Lesson 1 of 6

vectorization vs loops

Vectorized operations run in fast compiled code, while Python loops over rows are far slower.

In this page:

  1. vectorization vs loops
Syntax
python
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

python
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
  1. Using iterrows for calculations
  2. Growing DataFrames row by row
  3. 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:

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.