Fundamentals 5 min read

Why Using loc/iloc in Loops Is Slow and How at/iat Provides Faster Alternatives in Pandas

This article explains why pandas loc/iloc operations inside Python loops are extremely slow, demonstrates the performance difference with concrete examples, and shows that using the scalar‑access methods at and iat can speed up the same task by dozens of times.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Why Using loc/iloc in Loops Is Slow and How at/iat Provides Faster Alternatives in Pandas

When iterating over a pandas DataFrame, using the loc or iloc accessor to read or write values can take a very long time; the article investigates the cause and presents faster alternatives.

loc[] is a pandas function that accesses DataFrame values by row label and column name. For example, given a DataFrame df , df.loc[1, 'a'] returns the value 10.

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

df.loc[1,'a']

### 输出:10</code>

iloc works similarly but uses integer positions instead of labels, e.g., df.iloc[1,0] also returns 10.

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

df.iloc[1,0]

### 输出:10</code>

Because loc accesses by column name and iloc by column index, they are suitable for vectorized operations on whole rows or columns, not for scalar updates inside loops.

Example: adding a new column c as the sum of columns a and b using a for loop with loc :

<code>import timestart = time.time()

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

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

### 所用时间:2414 秒</code>

This loop takes about 40 minutes.

Replacing loc with the scalar accessor at (or iloc with iat ) yields a dramatic speedup:

<code>import timestart = time.time()

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

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

### 所用时间:40 秒</code>

The at / iat version runs in about 0.7 minutes, roughly 60 times faster than the loc version.

The reason is that at and iat are designed for scalar access, making them lightweight and fast, whereas loc and iloc are built for accessing Series or DataFrames and involve additional overhead.

Therefore, using loc / iloc inside Python loops is not recommended; for element‑wise updates, prefer at / iat to achieve much better performance.

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.