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

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

Last updated: February 19, 2024

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.

Next Article: Understanding pandas.DataFrame.loc[] through 6 examples

Previous Article: Pandas: Retrieve the first/last N rows of a DataFrame

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)