Python:Pandas .at[]

aSagCoder's avatar
Published Sep 1, 2024
Contribute to Docs

In Pandas, the .at[] function is used as an accessor to fetch a specific value from a DataFrame using row and column pairs.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

dataframe.at[index, label]
  • index: The index (or row label) where the specific value is located or where you want to set the value.
  • label: The label (or column name) where the specific value is located or where you want to set the value.

The result returned is a single element located at the specified position within the DataFrame.

Example

The following example shows the use of the .at[] accessor function:

import pandas as pd
# Create a DataFrame with two columns 'A' and 'B'
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
# Use .at[] to access the element at row 0, column 'B'
element = df.at[0, 'B']
# Print the accessed element
print(element)

The output of the code is as follows:

3

Codebyte Example

Run the following codebyte to understand how the .at[] accessor is used to access a specific element in a DataFrame at a given row and column label:

Code
Output

All contributors

Contribute to Docs

Learn Python:Pandas on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours