Menu Close

Mastery Pandas at and iat for Data Selection

Pandas at and iat

Pandas is a powerful Python library for data manipulation and analysis, and Pandas at and iat offers many ways to access and modify data. Pandas provides two methods to access and manipulate data in Pandas DataFrame.

These methods provide fast and straightforward ways to access and update data at specific locations within a DataFrame. This article will cover the differences between .at and .iat, how to use them with examples, and the advantages they offer in performance and code readability.

What Are Pandas .at and .iat?

The .at and .iat accessors in Pandas allow you to access specific values in a DataFrame using labels and integer-based indexing. They are optimized for fast, single-element access, making them faster than the more general .loc and .iloc accessors when you need to access or modify individual cells.

  • .at is label-based: It allows you to access a single value at a specific row and column label.
  • .iat is integer-based: It lets you access a single value at a specific row and column position using zero-based integer indices.

Syntax of .at and .iat in Pandas

Here is the basic syntax of at and iat methods.

.at Syntax

The .at accessor is used for label-based access, where you specify the row and column labels.

# Access a single value at row and column label
df.at[row_label, column_label]

# Modify a single value at row and column label
df.at[row_label, column_label] = new_value

.iat Syntax

The .iat accessor is used for integer-based access, where you specify the row and column index positions (0-based).

# Access a single value at row and column index position
df.iat[row_index, column_index]

# Modify a single value at row and column index position
df.iat[row_index, column_index] = new_value

Using .at and .iat in Pandas

Let’s walk through examples to understand how .at and .iat work in practice. To apply .at and .iat I have created a sample Pandas DataFrame as you can see below.

import pandas as pd
# Creating a DataFrame from a list of dictionaries
data = [
    {'Name': 'Alice', 'Age': 25, 'Gender': 'F', 'Score': 100},
    {'Name': 'Bob', 'Age': 30, 'Gender': 'M', 'Score': 60},
    {'Name': 'Charlie', 'Age': 35, 'Gender': 'M', 'Score': 70}
]
df = pd.DataFrame(data, index=['a', 'b', 'c'])
print(df)

Output

      Name  Age Gender  Score
a    Alice   25      F    100
b      Bob   30      M     60
c  Charlie   35      M     70

1. Accessing Elements with .at

The .at accessor is ideal when you want to access elements by their row and column labels.

Example: Access a Single Value

To access the value in the first row (a) and column Name:

value = df.at['a', 'Name']
print(value)

The Output will be Alice.

Example: Update a Single Value

In this example, I am changing the row b‘s value for column Age.

df.at['b', 'Age'] = 35
print(df)

Output

      Name  Age Gender  Score
a    Alice   25      F    100
b      Bob   35      M     60
c  Charlie   35      M     70

2. Accessing Elements with .iat

The .iat accessor works similarly to .at but requires integer indices for rows and columns, similar to Python’s native indexing.

Example: Access a Single Value

In the example below, I am accessing the value of the third row (index 2) and column Age (index 1).

value = df.iat[2, 1]
print(value)

Example: Update a Single Value

To change the value at row index 1 and column index 2:

df.iat[1, 3] = 120
print(df)

Output

      Name  Age Gender  Score
a    Alice   25      F    100
b      Bob   30      M    120
c  Charlie   35      M     70

This is how you can use at and iat to select and update a single value from Pandas DataFrame.

When to Use .at and .iat

Use .at when:

  • You need to access or modify a single element by label.
  • You know the row and column names but not their integer positions.

Use .iat when:

  • You want to access or modify a single element by index.
  • You know the integer positions of the row and column.

See Also:


Conclusion

In summary, Pandas at and iat are two essential tools to select and modify single elements from Pandas DataFrame.They offer a straightforward and performance-optimized way to work with single cells, making them particularly useful for applications where precise, high-speed data manipulation is required.

This is written and verified by Vishvajit Rao.

Thanks for Reading.

Mastery Data Selection: Loc and iLoc in Pandas

Related Posts