Fundamentals 5 min read

Understanding pandas loc/iloc Performance and Faster Alternatives with at/iat

This article explains how pandas loc and iloc work, demonstrates their slow performance when used inside loops, and shows that using the at and iat accessors provides a dramatically faster alternative for scalar assignments in DataFrames.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding pandas loc/iloc Performance and Faster Alternatives with at/iat

When working with pandas DataFrames, the loc[] function is used to access values by row label and column name, while iloc[] accesses by integer position. The article begins with a simple example showing how to retrieve the value 10 from column "a" in the second row using df.loc[1, 'a'] and the equivalent df.iloc[1, 0] .

It then illustrates the performance problem that arises when loc (or iloc ) is used inside a Python for loop to create a new column "c" as the sum of columns "a" and "b". The loop iterates over each row with df.iterrows() and assigns the result with df.loc[index, 'c'] = row.a + row.b . The measured execution time is about 40 minutes (2414 seconds), demonstrating that this approach is inefficient.

<code>##df.loc[index, column_name]

df.loc[1,'a']
### 输出:10
</code>

As a faster alternative, the article recommends replacing loc with at (or iloc with iat ) for scalar assignments. Using df.at[index, 'c'] = row.a + row.b reduces the execution time to roughly 40 seconds, a speed‑up of about 60×.

<code>##df.at[index, column_name]

df.at[1,'a']
### 输出:10
</code>

The reason for the speed difference is explained: at / iat are lightweight accessors designed for single scalar values, while loc / iloc support vectorized operations on multiple rows or columns, incurring additional overhead.

In conclusion, the article advises avoiding loc / iloc inside loops for element‑wise updates and instead using at / iat when possible, as they are significantly faster for scalar assignments.

performancepandasiloclocatiat
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.