Sub topic 14
Fast scalar access with .at and .iat
This chapter assumes you know .loc and .iloc from earlier in the tutorial. It also assumes you can build a DataFrame from a dictionary and that import pandas as pd is already in place.
When one value is all you need
A scalar is a single value, such as one number or one string. Often you want exactly one cell, for example the population of one city. .loc and .iloc can return a cell, but they also handle slices, lists, and boolean masks. That flexibility adds work on every call.
pandas provides two narrower accessors for single cells:
.atfinds a cell by row label and column label..iatfinds a cell by row position and column position.
They mirror .loc and .iloc. Each accepts exactly one row and one column, and each returns the value itself, not a Series or DataFrame.
Reading a value
The example below builds a small table and moves the city names into the index. Then it reads the same cell three ways.
import pandas as pd
df = pd.DataFrame(
{
"city": ["Oslo", "Lima", "Pune"],
"country": ["Norway", "Peru", "India"],
"population_k": [709, 10092, 3124],
}
)
by_city = df.set_index("city")
print(by_city.loc["Lima", "population_k"])
print(by_city.at["Lima", "population_k"])
print(by_city.iat[1, 1])All three lines print 10092. In the .iat call, row position 1 is Lima. Column position 1 is population_k, because city became the index and is no longer a column. Positions count only the columns that remain.
Writing a value
Both accessors also assign to a single cell.
by_city.at["Oslo", "population_k"] = 715
by_city.iat[2, 1] = 3200
print(by_city)The first line sets Oslo's population by label. The second sets Pune's population by position. Neither call creates a copy, so there is no chained-assignment risk.
Choosing an accessor
The speed gain is real but small per call. It matters mainly inside a Python loop that touches many cells one at a time. For a single lookup in interactive work, .loc is just as fine and more familiar. If you find yourself looping over cells, check whether a vectorized operation on a whole column would replace the loop.
Common errors
| Mistake | What happens |
|---|---|
| `by_city.at["Lima":"Pune", "population_k"]` | Slices are not allowed. pandas raises an error. |
| `by_city.at[["Lima"], "population_k"]` | Lists are not allowed. pandas raises an error. |
| `by_city.at["Rome", "population_k"]` | A label that does not exist raises `KeyError`. |
| `by_city.iat[10, 0]` | A position outside the table raises `IndexError`. |
| `by_city.iat["Lima", 1]` | `.iat` accepts only integers. Using a label raises an error. |
Duplicate labels need extra care. If the index contains "Lima" twice, .at["Lima", "population_k"] can return more than one value instead of a scalar. Check by_city.index.is_unique before relying on .at for lookups.
Summary
.attakes one row label and one column label..iattakes one row position and one column position.- Both read and write a single cell.
- Neither accepts slices, lists, or masks. Use
.locor.ilocfor those.
Exercise
You have by_city indexed by city name, with columns country and population_k in that order. Write one line using .iat that reads Pune's country if Pune is the third row. Then write the equivalent line using .at.
Show answer
by_city.iat[2, 0] and by_city.at["Pune", "country"]. Row position 2 is the third row. Column position 0 is country, the first column after city became the index. Both return "India".