Introduction to pandas DataFrame Iteration in Python 2026 with Efficient Code
Iterating over pandas DataFrames is one of the most common — and most misunderstood — tasks in data analysis. In 2026, knowing the right way to iterate (or better yet, avoid iterating) is crucial for writing fast and efficient code.
This March 15, 2026 guide explains the different iteration methods and when to use (or avoid) each one.
TL;DR — Key Takeaways 2026
- Never use
iterrows()in production code — it is very slow - Prefer vectorized operations over any form of iteration
- Use
.apply()sparingly and only when vectorization isn’t possible itertuples()is significantly faster thaniterrows()- The best code often has no explicit iteration at all
1. The Iteration Methods Compared
import pandas as pd
import numpy as np
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'score': [85, 92, 78]
})
# 1. iterrows() - Slowest (Avoid in 2026)
for index, row in df.iterrows():
print(row['name'], row['score'])
# 2. itertuples() - Much faster
for row in df.itertuples():
print(row.name, row.score)
# 3. Vectorized (Best approach)
df['score_double'] = df['score'] * 2
df['age_group'] = np.where(df['age'] >= 30, 'Senior', 'Junior')
2. Performance Ranking 2026
| Method | Speed | Readability | Recommendation |
|---|---|---|---|
| Vectorized operations | Extremely Fast | Excellent | Best Choice |
itertuples() | Fast | Good | Acceptable when needed |
.apply() | Medium | Good | Use sparingly |
iterrows() | Very Slow | Poor | Avoid in production |
3. Modern Efficient Patterns
# Best practices in 2026
# 1. Vectorized calculations
df['total'] = df['score'] + df['age'] * 10
# 2. Conditional logic with np.where
df['status'] = np.where(df['score'] >= 90, 'Excellent',
np.where(df['score'] >= 75, 'Good', 'Needs Improvement'))
# 3. When you really need row-wise logic
def calculate_bonus(row):
return row['score'] * 0.1 if row['score'] > 85 else 0
# Still better than iterrows:
df['bonus'] = df.apply(calculate_bonus, axis=1)
The Iteration Options at a Glance
pandas offers several ways to go through a DataFrame row by row: .iloc indexing in a loop, .iterrows(), .itertuples() and .apply(axis=1). They differ greatly in speed because each creates different objects per row.
iterrows builds a Series for every row, which is slow and can change data types. itertuples builds lightweight named tuples and is much faster. Vectorised column operations avoid row iteration completely and are fastest of all.
A good rule is to try a vectorised solution first, then itertuples if row logic is unavoidable.
Worked Example: Comparing Iteration Methods
The same row-level calculation is timed with four approaches.
import timeit
import numpy as np
import pandas as pd
rng = np.random.default_rng(1)
df = pd.DataFrame({"price": rng.uniform(1, 100, 20_000), "qty": rng.integers(1, 10, 20_000)})
def with_iloc():
return [df.iloc[i]["price"] * df.iloc[i]["qty"] for i in range(len(df))]
def with_iterrows():
return [row["price"] * row["qty"] for _, row in df.iterrows()]
def with_itertuples():
return [row.price * row.qty for row in df.itertuples()]
def vectorised():
return (df["price"] * df["qty"]).tolist()
print(np.allclose(with_itertuples(), vectorised()))
for f in (with_iloc, with_iterrows, with_itertuples, vectorised):
print(f"{f.__name__:16} {min(timeit.repeat(f, number=1, repeat=3)):.4f}s")
All methods compute the same revenue values. On a typical machine the order from slowest to fastest is iloc, iterrows, itertuples and vectorised, with the vectorised version hundreds of times faster than iloc.
Practical Tips
Before choosing a method, ask whether the rows really need to be visited one at a time. Most row loops in data scripts compute something that can be written as a column expression, a groupby or a merge.
When a loop is unavoidable, move any work that does not depend on the row outside it, such as compiling patterns or loading lookup tables.
Common Mistakes
- Defaulting to iterrows for every task.
- Using iloc inside loops.
- Modifying a DataFrame while iterating over it.
- Ignoring a vectorised alternative.
Quick Summary
- Row iteration methods differ widely in speed.
- iterrows is slow and may change dtypes.
- itertuples is the fastest row loop.
- Vectorised operations beat all loops.
Conclusion — pandas DataFrame Iteration in 2026
The golden rule in modern pandas is: “If you’re iterating over a DataFrame, you’re probably doing it wrong.”
In 2026, the most efficient pandas code uses vectorized operations, boolean indexing, and built-in pandas/NumPy functions. Reserve explicit iteration only for rare cases where vectorization is truly impossible.
Next steps:
- Review your pandas code and replace
iterrows()and most.apply()calls with vectorized operations