0
0
PandasHow-ToBeginner · 3 min read

How to Use at and iat in pandas for Fast Data Access

In pandas, use at to quickly access or set a single value by label (row and column names), and use iat to do the same by integer position (row and column index). Both are faster than general indexing methods when working with single scalar values.
📐

Syntax

at accesses a single value by row and column labels, while iat accesses a single value by integer position (row and column indices).

  • df.at[row_label, column_label] - get or set value by labels
  • df.iat[row_index, column_index] - get or set value by integer positions
python
value = df.at[row_label, column_label]
value = df.iat[row_index, column_index]
df.at[row_label, column_label] = new_value
df.iat[row_index, column_index] = new_value
💻

Example

This example shows how to use at and iat to get and set single values in a pandas DataFrame.

python
import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data, index=['a', 'b', 'c'])

# Access value by label using at
age_bob = df.at['b', 'Age']

# Access value by integer position using iat
name_charlie = df.iat[2, 0]

# Set value by label using at
df.at['a', 'Age'] = 26

# Set value by integer position using iat
df.iat[1, 0] = 'Bobby'

print('Age of Bob:', age_bob)
print('Name at row 2, col 0:', name_charlie)
print('\nUpdated DataFrame:')
print(df)
Output
Age of Bob: 30 Name at row 2, col 0: Charlie Updated DataFrame: Name Age a Alice 26 b Bobby 30 c Charlie 35
⚠️

Common Pitfalls

Common mistakes when using at and iat:

  • Using at with integer positions instead of labels causes errors.
  • Using iat with labels instead of integer positions causes errors.
  • Trying to access multiple values with at or iat will fail; they only work for single scalar values.
  • Index or column labels must exist when using at, or it raises a KeyError.

Wrong usage example:

python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['x', 'y'])

# Wrong: using at with integer position
try:
    print(df.at[0, 1])
except Exception as e:
    print('Error:', e)

# Wrong: using iat with labels
try:
    print(df.iat['x', 'B'])
except Exception as e:
    print('Error:', e)

# Correct usage
print('Correct at:', df.at['x', 'B'])
print('Correct iat:', df.iat[0, 1])
Output
Error: 0 Error: iat requires integer position(s) Correct at: 3 Correct iat: 3
📊

Quick Reference

MethodAccess TypeParametersUse Case
atLabel-basedrow_label, column_labelGet or set single value by labels
iatInteger position-basedrow_index, column_indexGet or set single value by integer positions

Key Takeaways

Use df.at[row_label, col_label] for fast label-based single value access or assignment.
Use df.iat[row_index, col_index] for fast integer position-based single value access or assignment.
Both at and iat only work with single scalar values, not slices or multiple values.
Using wrong types (labels with iat or positions with at) causes errors.
They are faster than loc or iloc when working with single values.