Iterating with .iterrows() in pandas – Why You Should Avoid It in 2026

Efficient Code Mar 15, 2026

Iterating with .iterrows() in pandas – Why You Should Avoid It in 2026

df.iterrows() is one of the most commonly used — and most criticized — methods in pandas. While it looks convenient, it is notoriously slow and should be avoided in almost all cases in 2026.

This March 15, 2026 guide explains why .iterrows() is slow and shows the modern, efficient alternatives you should use instead.

TL;DR — Key Takeaways 2026

  • .iterrows() is very slow because it returns a Series for each row
  • It is one of the worst ways to iterate over a DataFrame
  • Prefer vectorized operations, .itertuples(), or .apply() (sparingly)
  • Vectorized code is typically 50–200x faster than .iterrows()
  • Never use .iterrows() in production code unless absolutely necessary

1. The Problem with .iterrows()


import pandas as pd

df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'score': [85, 92, 78]
})

# ❌ Very slow and not Pythonic
for index, row in df.iterrows():
    df.loc[index, 'score_double'] = row['score'] * 2

2. Better Alternatives in 2026


# ✅ Option 1: Vectorized (Recommended)
df['score_double'] = df['score'] * 2

# ✅ Option 2: itertuples() - Much faster than iterrows
for row in df.itertuples():
    # row is a namedtuple - very fast access
    pass

# ✅ Option 3: When you really need row-wise logic
df['score_double'] = df.apply(lambda row: row['score'] * 2, axis=1)

3. Performance Comparison 2026

Method Relative Speed Recommendation
Vectorized operations100x (fastest)Best choice
.itertuples()20–50xAcceptable when needed
.apply(axis=1)5–15xUse sparingly
.iterrows()1x (slowest)Avoid in production

4. Best Practices in 2026

  • Never use .iterrows() for performance-critical code
  • Default to vectorized operations whenever possible
  • Use .itertuples() when you must iterate row by row
  • Use .apply() only when vectorization is not feasible
  • Profile first — then decide if iteration is truly needed

What iterrows Really Returns

iterrows yields an index and a Series for each row. Building that Series costs time, and because a Series has a single dtype, mixed rows are upcast: integers can become floats when the row also contains a float.

That type change can cause subtle bugs, such as an id column turning into 101.0. itertuples keeps each column's type and is much faster, so it is the better choice for row loops.

Changes made to the row Series do not write back to the DataFrame, which surprises many beginners.

Worked Example: Type Changes and Missing Writes

We show the dtype change and the lost update.

import pandas as pd

df = pd.DataFrame({"order_id": [101, 102], "amount": [19.99, 5.0]})

for _, row in df.iterrows():
    print(type(row["order_id"]).__name__, row["order_id"])

for row in df.itertuples():
    print(type(row.order_id).__name__, row.order_id)

for _, row in df.iterrows():
    row["amount"] = 0
print(df["amount"].tolist())

With iterrows, order ids come back as floats such as 101.0, while itertuples keeps them as integers. Setting row["amount"] inside the loop leaves the DataFrame unchanged, so the amounts are still 19.99 and 5.0.

Practical Tips

If you still need iterrows for a quick exploration, use it on a small sample, such as df.head(), and switch to a better method before the code runs on full data.

Writing results back should happen after the loop: collect values in a list and assign the list as a new column. For conditional updates, df.loc with a boolean mask changes all matching rows in one vectorised step, which is both faster and safer than modifying rows one at a time.

In short, treat iterrows as a debugging aid rather than a production tool.

Replacing it with vectorised code or itertuples is usually a small change with a large payoff, and the resulting code is often shorter as well.

Common Mistakes

  • Expecting iterrows to preserve dtypes.
  • Trying to modify the DataFrame through the row.
  • Using iterrows on large tables.
  • Ignoring vectorised alternatives.

Quick Summary

  • iterrows builds a Series per row.
  • Mixed rows are upcast.
  • Row changes do not write back.
  • Prefer itertuples or vectorised code.

Conclusion — Iterating with .iterrows() in 2026

.iterrows() is one of the slowest ways to iterate over a pandas DataFrame. In 2026, the most efficient pandas code almost never uses .iterrows(). Instead, it relies on vectorized operations, boolean indexing, and occasionally .itertuples() when row-by-row processing is unavoidable.

Next steps:

  • Search your codebase for .iterrows() and replace it with vectorized alternatives

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices