Pandas DataFrame.iat


Pandas DataFrame.iat

The DataFrame.iat property in pandas is used to access a single value in a DataFrame by row and column position (integer-based indexing). It is similar to iloc but is optimized for getting and setting individual values.


Syntax

The syntax for accessing the iat property is:

DataFrame.iat[row, column]

Here, row and column are integer indices specifying the row and column positions in the DataFrame.


Parameters

  • row: An integer representing the row index.
  • column: An integer representing the column index.

Returns

The value at the specified position in the DataFrame.


Examples

Accessing a Single Value

Use iat to access a single value in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)

# Access the value at row 1, column 2 (0-based indexing)
print("Value at row 1, column 2:")
print(df.iat[1, 2])

Output

Value at row 1, column 2:
80000.0

Setting a Single Value

Use iat to modify a single value in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)

# Modify the value at row 0, column 1
print("Before modification:")
print(df)
df.iat[0, 1] = 28
print("\nAfter modification:")
print(df)

Output

Before modification:
    Name  Age   Salary
0  Arjun   25  70000.5
1    Ram   30  80000.0
2   Priya   35  90000.0

After modification:
    Name  Age   Salary
0  Arjun   28  70000.5
1    Ram   30  80000.0
2   Priya   35  90000.0

Accessing Values in a Larger DataFrame

Use iat to quickly access values in large DataFrames.

Python Program

import pandas as pd
import numpy as np

# Create a large DataFrame
data = np.random.randint(1, 100, size=(100, 5))
columns = ['A', 'B', 'C', 'D', 'E']
df = pd.DataFrame(data, columns=columns)

# Access a specific value
print("Value at row 10, column 3:")
print(df.iat[10, 3])

Output

Value at row 10, column 3:

Performance Comparison with iloc

Compare the performance of iat and iloc for accessing single values.

Python Program

import pandas as pd
import numpy as np
import time

# Create a large DataFrame
data = np.random.randint(1, 100, size=(100000, 10))
df = pd.DataFrame(data)

# Measure time for iat
start = time.time()
value = df.iat[999, 5]
iat_time = time.time() - start

# Measure time for iloc
start = time.time()
value = df.iloc[999, 5]
iloc_time = time.time() - start

print(f"Time using iat: {iat_time:.10f} seconds")
print(f"Time using iloc: {iloc_time:.10f} seconds")

Output

Time using iat: 

Summary

In this tutorial, we explored the DataFrame.iat property in pandas. Key takeaways include:

  • Using iat for fast, integer-based access to single values.
  • Modifying specific values in a DataFrame.
  • Understanding the performance advantages of iat over iloc for single-value operations.

The DataFrame.iat property is an efficient and convenient tool for working with individual values in pandas DataFrames.


Python Libraries