How can I get a value from a cell of a dataframe?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting one value from a pandas DataFrame is a small task, but pandas offers several ways to do it and they are not interchangeable. The best choice depends on whether you are indexing by labels or integer positions and whether you want a guaranteed scalar result instead of a Series or DataFrame slice.
Use .at for One Label-Based Cell
If you know the row label and the column label, .at is the most direct API for a single scalar lookup.
This returns 88.
.at is a good default when the DataFrame has meaningful row labels and you are retrieving exactly one cell.
Use .iat for One Position-Based Cell
If you know the integer position of the row and column, use .iat.
This also returns 88, but it uses zero-based integer positions instead of labels.
.iat is usually faster and clearer than .iloc when you only want one scalar cell.
.loc and .iloc Also Work
You can retrieve a single value with .loc or .iloc, but they are more general-purpose indexers.
Label-based:
Position-based:
These are perfectly valid, and many developers prefer them because the syntax is consistent with wider row and column selection. The tradeoff is that .at and .iat signal more clearly that you want a single scalar value.
Understanding Labels vs Positions
A lot of confusion comes from mixing labels and integer positions.
Consider this DataFrame:
Now:
Both lines return 20, but for different reasons:
- '
.at[200, "value"]uses row label200' - '
.iat[1, 0]uses the second row by position'
That distinction becomes critical once your index is not the default 0, 1, 2, ... range.
Avoid Chained Indexing for One Cell
Beginners often write code like this:
It may work, but it is not the best pattern. Chained indexing can be ambiguous and is more error-prone, especially when the code later evolves from reading to writing.
For single-cell access, .at, .iat, .loc, or .iloc are clearer and more robust.
Handling Missing Labels Safely
If a row label or column name may be absent, direct scalar access can raise KeyError or IndexError. In those cases, check membership first or handle the exception explicitly.
This matters when working with user-selected columns or DataFrames produced by variable upstream pipelines.
Scalar Retrieval Inside Loops
If you need one value occasionally, scalar access is fine. If you are pulling cell values inside a large Python loop, it may be a sign that the work should be vectorized instead.
For example, instead of reading each row’s cell value one by one, many operations can be done with a column expression:
Pandas is usually most efficient when you operate on whole columns or arrays rather than performing thousands of scalar lookups in Python.
Common Pitfalls
The biggest pitfall is confusing labels with positions. .at and .loc use labels, while .iat and .iloc use integer positions.
Another issue is chained indexing such as df["col"][row], which is less explicit and more fragile than dedicated indexers.
Developers also sometimes expect .loc to always return a scalar, but duplicated labels can produce a Series or DataFrame instead.
Finally, repeated scalar access inside loops can be a performance smell in pandas code.
Summary
- Use
.at[row_label, column_label]for one label-based cell. - Use
.iat[row_index, column_index]for one position-based cell. - '
.locand.ilocalso work, but they are broader indexing tools.' - Be careful about the difference between labels and zero-based positions.
- Prefer vectorized pandas operations when you find yourself doing many scalar lookups in loops.

