🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.at[]

AI & DATA SCIENCE // df-at

df.at[] accesses (or sets) a single scalar value by row label and column label, optimized specifically for fast single-value label-based access.

Syntax

df.at[row_label, column_label]

Deep Dive Course

at[] is a scalar-only, faster specialization of loc[]: where loc[] can select whole rows, columns, or ranges depending on what you pass it, at[] can only ever get or set exactly one value, and it skips the general-purpose overhead loc[] needs to support that broader flexibility. This makes at[] the right choice specifically when you already know you're accessing a single cell by label, especially inside a loop where that overhead difference actually adds up.

1Understanding df.at[]

at[] is a scalar-only, faster specialization of loc[]: where loc[] can select whole rows, columns, or ranges depending on what you pass it, at[] can only ever get or set exactly one value, and it skips the general-purpose overhead loc[] needs to support that broader flexibility. This makes at[] the right choice specifically when you already know you're accessing a single cell by label, especially inside a loop where that overhead difference actually adds up.

💡

Use at[] over loc[] specifically when you know you're reading or writing exactly one scalar value by label — it's measurably faster for that specific case, especially when done repeatedly in a loop, since it skips loc[]'s more general-purpose selection machinery.

editor.html
import pandas as pd

df = pd.DataFrame({"score": [85, 90, 78]}, index=["a", "b", "c"])
print(df.at["b", "score"])
localhost:3000

2Practical Example

Here is a real-world application of df.at[] showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"score": [85, 90, 78]}, index=["a", "b", "c"])
df.at["c", "score"] = 100
print(df)
localhost:3000

3Best Practices

Follow these guidelines when working with df.at[]:

1. Use at[] instead of loc[] for single-scalar access/assignment when performance matters, such as inside a loop

2. Use loc[] instead of at[] whenever you might select more than a single value, a whole row, column, or range

3. Use iat[] instead of at[] when you're accessing by integer position rather than by label

⚠️

Tip: Use at[] over loc[] specifically when you know you're reading or writing exactly one scalar value by label — it's measurably faster for that specific case, especially when done repeatedly in a loop, since it skips loc[]'s more general-purpose selection machinery.

editor.html
import pandas as pd

df = pd.DataFrame({"score": [85, 90, 78]}, index=["a", "b", "c"])
print(df.at["b", "score"])
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"score": [85, 90, 78]}, index=["a", "b", "c"])
print(df.at["b", "score"])
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"score": [85, 90, 78]}, index=["a", "b", "c"])
df.at["c", "score"] = 100
print(df)

Best Practices

  • Use at[] instead of loc[] for single-scalar access/assignment when performance matters, such as inside a loop
  • Use loc[] instead of at[] whenever you might select more than a single value, a whole row, column, or range
  • Use iat[] instead of at[] when you're accessing by integer position rather than by label

Interview Question

Why is df.at['row', 'col'] generally faster than df.loc['row', 'col'] for getting a single value, given they're accessing the exact same cell?

Hint: Think about how much flexibility each accessor has to support, and what that flexibility costs.

loc[] has to support a much wider range of possible selections — whole rows, whole columns, label-based slices, boolean masks, and single scalars all through the same general-purpose interface, which requires extra internal logic to figure out what kind of selection you're actually asking for each time it's called. at[] is deliberately restricted to only ever handling the single-scalar case, so it can skip all of that general-purpose dispatch logic and go directly to retrieving one value, which is measurably faster, especially when the access happens repeatedly, such as inside a loop over many rows.

Exercises

MediumPractice using df.at[] in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"score": [85, 90, 78]}, index=["a", "b", "c"])
print(df.at["b", "score"])

Frequently Asked Questions

Why is df.at['row', 'col'] generally faster than df.loc['row', 'col'] for getting a single value, given they're accessing the exact same cell?

loc[] has to support a much wider range of possible selections — whole rows, whole columns, label-based slices, boolean masks, and single scalars all through the same general-purpose interface, which requires extra internal logic to figure out what kind of selection you're actually asking for each time it's called. at[] is deliberately restricted to only ever handling the single-scalar case, so it can skip all of that general-purpose dispatch logic and go directly to retrieving one value, which is measurably faster, especially when the access happens repeatedly, such as inside a loop over many rows.

Related Functions

df-locdf-iatdf-iloc