Skip to content

Suggest replacing df.iterrows() with df.itertuples() for better performance #1

Description

@SaFE-APIOpt

for index,row in df.iterrows():

Current code:

for index, row in df.iterrows():
    ...

Recommended replacement:

for row in df.itertuples(index=True):
    ...

iterrows() constructs each row as a Pandas Series, resulting in significant overhead due to object creation, index binding, and repeated type inference. This becomes increasingly inefficient with larger DataFrames.

In contrast, itertuples() yields lightweight namedtuples at the Cython level, allowing for faster row-wise access with minimal memory usage. Benchmark results show that itertuples() is up to 50x faster on large datasets while producing the same output structure for read-only access.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions