pandas
dataframe
data manipulation
python
cell value extraction

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.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Ada", "Grace", "Linus"],
6        "score": [95, 88, 91],
7    },
8    index=["a", "b", "c"],
9)
10
11value = df.at["b", "score"]
12print(value)

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.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Ada", "Grace", "Linus"],
6        "score": [95, 88, 91],
7    }
8)
9
10value = df.iat[1, 1]
11print(value)

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:

python
value = df.loc["b", "score"]

Position-based:

python
value = df.iloc[1, 1]

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:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {"value": [10, 20, 30]},
5    index=[100, 200, 300],
6)

Now:

python
print(df.at[200, "value"])
print(df.iat[1, 0])

Both lines return 20, but for different reasons:

  • '.at[200, "value"] uses row label 200'
  • '.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:

python
value = df["score"]["b"]

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.

python
1if "b" in df.index and "score" in df.columns:
2    print(df.at["b", "score"])
3else:
4    print("Cell does not exist")

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:

python
df["passed"] = df["score"] >= 90

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.
  • '.loc and .iloc also 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.

Course illustration
Course illustration

All Rights Reserved.