Fundamentals 6 min read

Understanding pandas loc/iloc vs at/iat: Performance Comparison and Best Practices

This article explains the differences between pandas loc/iloc and at/iat, demonstrates why using loc/iloc inside loops can be extremely slow, and shows how replacing them with at/iat can speed up DataFrame updates by up to 60 times.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding pandas loc/iloc vs at/iat: Performance Comparison and Best Practices

While experimenting with Python loops, the author noticed that using iloc or loc inside a loop on a pandas DataFrame consumes a large amount of time. The article investigates why loc is slow and presents faster alternatives.

What is loc ?

The loc[] function is a pandas accessor that retrieves values from a DataFrame using row labels and column names when both are known.

Example: given a DataFrame df , to get the value in column 'a' at the second row (index 1), use:

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

df.loc[1,'a']

### Output: 10</code>

Similarly, iloc accesses values by integer position:

<code>##df.iloc[index, column_number]

df.iloc[1,0]

### Output: 10</code>

Thus, loc works with column **names**, while iloc works with column **indices**.

What happens when loc / iloc are used inside a loop?

Suppose we want to add a new column 'c' to df whose values are the sum of columns 'a' and 'b' . Using a for loop with loc :

<code>import time
start = time.time()

# iterate over DataFrame df
for index, row in df.iterrows():
    df.loc[index, 'c'] = row.a + row.b

end = time.time()
print(end - start)

### Time taken: 2414 seconds</code>

The loop with loc takes about 40 minutes, which is excessively long.

Alternative: use at instead of loc

Replacing loc with at (or iloc with iat ) yields the same result much faster:

<code>import time
start = time.time()

# iterate over DataFrame
for index, row in df.iterrows():
    df.at[index, 'c'] = row.a + row.b

end = time.time()
print(end - start)

### Time taken: 40 seconds</code>

This version runs in roughly 0.7 minutes, about 60 times faster than the loc version.

Why do loc / iloc and at / iat differ in runtime?

at / iat : Designed for scalar (single‑element) access, making them lightweight and fast.

loc / iloc : Intended for accessing multiple elements (Series or DataFrames) and may perform vectorized operations, which adds overhead.

Because at accesses a scalar directly, it avoids the extra processing required by loc , resulting in significantly lower execution time.

For further reading, see the recommended articles linked at the end of the original post.

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