Fundamentals 4 min read

Speeding Up Pandas Row Iteration: Use itertuples() Instead of iterrows()

This article explains why iterating over Pandas DataFrames with iterrows() is slow, demonstrates the performance gain of using itertuples()—often over a hundred times faster—and provides a simple example to help Python data‑analysis practitioners write more efficient code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Speeding Up Pandas Row Iteration: Use itertuples() Instead of iterrows()

The author reflects on common complaints about Python's speed compared to compiled languages like C or Go, shares personal doubts about Python's performance, and notes attempts to use Go for data‑science tasks, which felt less pleasant due to static typing.

Motivated to improve daily Pandas work, the article focuses on speeding up row‑wise operations, specifically iterating over DataFrame rows.

A simple dataset is created: a single‑column DataFrame containing integers from 1 to 100,000, and its first few rows and shape are displayed to confirm the setup.

The author warns against the common but inefficient practice of using iterrows() for row iteration, explaining that it is slow by default and often overused.

A quick demonstration using %%time shows that iterating 100,000 rows with iterrows() and incrementing a variable takes around 15 seconds, highlighting the performance issue.

As a better alternative, the article introduces itertuples() , which returns named tuples for each row, allowing attribute‑style access (using the dot operator) and providing significant speed improvements.

Repeating the same task with itertuples() yields roughly a 154‑fold speed increase, illustrating how a small code change can dramatically reduce execution time when processing large numbers of rows.

The author concludes that while itertuples() may not always be 150× faster in every scenario, it generally offers a noticeable performance boost, emphasizing the importance of choosing efficient iteration methods in Pandas workflows.

Performancedata analysispandasiterrowsitertuples
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.