Pandas DataFrame: Access and modify the value of a cell with .at[] and .iat[]

Updated: February 19, 2024 By: Guest Contributor Post a comment

Introduction

When working with data in Python, Pandas is a powerhouse that provides numerous functionalities for data manipulation and analysis. Among its many features, accessing and modifying cells in a DataFrame is fundamental. Two methods that shine in terms of efficiency and convenience are .at[] and .iat[]. These methods allow you to retrieve or modify the value of a specific cell quickly. In this tutorial, we’ll explore how to use these methods with multiple code examples, ranging from basic to advanced usage.

Understanding .at[] and .iat[]

Pandas provides various ways to access and modify data, but when it comes to single elements, .at[] and .iat[] are your go-to options. .at[] provides label-based scalar lookups, making it ideal when you know the row and column labels of the cell you’re interested in. .iat[], on the other hand, is used for integer-location based indexing, so you’d use it when you know the row and column positions.

Accessing Values with .at[]

To start, let’s create a simple DataFrame. We’ll use this DataFrame throughout our examples:

import pandas as pd

# Sample DataFrame
data = {'Name': ['John', 'Lara', 'Mike'], 'Age': [28, 34, 23], 'City': ['New York', 'Paris', 'London']}
df = pd.DataFrame(data)
print(df)

The output will be:

    Name  Age      City
0  John   28  New York
1  Lara   34     Paris
2  Mike   23    London

Now, let’s access the age of John using .at[]:

print(df.at[0, 'Age'])

This will output:

28

Modifying Values with .at[]

To modify a value, you use the same method. If we want to update John’s age to 29, we would do:

df.at[0, 'Age'] = 29
print(df.at[0, 'Age'])

This will output:

29

Accessing and Modifying with .iat[]

Let’s say we want to update Lara’s city to ‘Berlin’. Using .iat[], knowing Lara’s row is 1 and the ‘City’ column is 2, we can:

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

This will output:

'Berlin'

Advanced Usage

Moving onto more advanced scenarios, let’s consider a DataFrame with a multi-index:

arrays = [['John', 'John', 'Lara', 'Mike'], ['2020', '2021', '2020', '2021']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Name', 'Year'])
data = {'Score': [88, 92, 95, 89]}
df = pd.DataFrame(data, index=index)
print(df)

This creates a DataFrame where each person has scores for different years. You can access specific scores using .at[] as follows:

print(df.at[('John', '2020'), 'Score'])

This outputs:

88

For modifying values in such a DataFrame, the process is similar. Let’s update John’s score for 2020:

df.at[('John', '2020'), 'Score'] = 90
print(df.at[('John', '2020'), 'Score'])

This will change the output to:

90

Performance Considerations

While .at[] and .iat[] are incredibly convenient for accessing and modifying a single cell’s value, they are optimized for performance. Using these methods is significantly faster than alternative methods when you need to manipulate individual elements. This speed advantage becomes particularly noticeable in large DataFrames or within loops.

Conclusion

In conclusion, understanding how to effectively use .at[] and .iat[] can greatly enhance your data manipulation capabilities in Pandas. These methods offer a precise way to access and modify the value of a single cell, combining ease of use with performance. Whether working with small data sets or large dataframes, mastering these tools can significantly streamline your data processing tasks.