.itertuples() in pandas – Fast Row Iteration in Python 2026 with Efficient Code
When you truly need to iterate over rows in a pandas DataFrame, .itertuples() is by far the fastest and most efficient option. In 2026, it remains the recommended method for row-wise iteration when vectorization is not possible.
This March 15, 2026 guide shows how to use .itertuples() effectively and why it is significantly better than .iterrows().
TL;DR — Key Takeaways 2026
.itertuples()is much faster than.iterrows()- It returns namedtuples, allowing fast attribute access
- Use it when you need row-by-row logic that cannot be vectorized
- Always prefer vectorized operations over any iteration when possible
- Free-threading safe and highly performant in modern Python
1. Basic Usage
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'score': [85, 92, 78],
'age': [25, 30, 35]
})
# ✅ Fast and efficient way
for row in df.itertuples():
print(f"Player: {row.name}, Score: {row.score}, Age: {row.age}")
2. Advanced Usage Patterns
# 1. With index
for row in df.itertuples(index=True):
print(row.Index, row.name, row.score)
# 2. Skipping index for speed
for row in df.itertuples(index=False):
if row.score > 85:
print(row.name, "is excellent")
# 3. Real-world calculation
total = 0
for row in df.itertuples():
total += row.score * row.age
print("Weighted total:", total)
3. Performance Comparison 2026
| Method | Relative Speed | Memory Usage | Recommendation |
|---|---|---|---|
| Vectorized operations | 100x (fastest) | Low | Best choice |
.itertuples() | 20–50x | Low | Best for row iteration |
.apply(axis=1) | 5–10x | Medium | Use sparingly |
.iterrows() | 1x (slowest) | High | Avoid |
4. Best Practices with .itertuples() in 2026
- Use
.itertuples()when you must iterate row by row - Use
index=Falsewhen you don’t need the index for extra speed - Prefer vectorized operations whenever possible
- Keep the loop body simple to maximize performance
- Consider
.apply()only for complex row-wise logic
How itertuples Works
itertuples yields one named tuple per row. Fields are accessed as attributes, such as row.amount, and the first field is the index unless index=False is passed. Named tuples are cheap to create, which is why itertuples is much faster than iterrows.
Column names that are not valid Python identifiers, such as names with spaces, are renamed to positional names like _1. Rename such columns first or access fields by position.
itertuples is read-only: collect results in a list and assign them as a new column after the loop.
Worked Example: Row Logic with Collected Results
Shipping fees depend on several columns, so a row loop is used and results are collected.
import pandas as pd
orders = pd.DataFrame({
"region": ["North", "South", "North", "West"],
"weight kg": [1.2, 7.5, 0.4, 3.0],
"express": [False, True, True, False],
})
orders = orders.rename(columns={"weight kg": "weight_kg"})
fees = []
for row in orders.itertuples(index=False):
fee = 5 if row.weight_kg < 2 else 9
if row.express:
fee *= 2
if row.region == "West":
fee += 3
fees.append(fee)
orders["fee"] = fees
print(orders)
Renaming "weight kg" first allows attribute access. The fees come out as 5, 18, 10 and 12, and they are assigned as a column once after the loop.
Practical Tips
itertuples has a name argument: name=None returns plain tuples, which are slightly faster when you access fields by position. Selecting only the needed columns first, as in df[["a", "b"]].itertuples(), also speeds up the loop.
When the loop calls an external service or writes to a file, the network or disk is usually the bottleneck, so the choice of iteration method matters less than batching those calls.
Profile one real run to see where the time actually goes before rewriting anything.
Remember that itertuples renames columns whose names are not valid Python identifiers, such as names containing spaces, so access them by position or rename the columns first.
Common Mistakes
- Accessing columns with spaces as attributes.
- Forgetting that the first field is the index.
- Trying to assign through the tuple.
- Using itertuples where a vectorised expression would work.
Quick Summary
- itertuples yields named tuples.
- Use index=False to drop the index field.
- Rename awkward column names first.
- Collect results, then assign once.
Conclusion — .itertuples() in pandas (2026)
.itertuples() is the fastest and most memory-efficient way to iterate over rows in a pandas DataFrame. While vectorized operations are always preferred, when iteration is truly necessary, .itertuples() is the clear winner over .iterrows() and .apply().
Next steps:
- Replace any
.iterrows()usage with.itertuples()or vectorized alternatives