Pandas DataFrames are essential for data manipulation in Python. Efficiently accessing individual cell values is a common task. This article explores several methods for retrieving these values, highlighting their strengths and weaknesses.
Table of Contents
- Integer-Based Indexing:
iloc
iat
andat
for Single-Cell Access- Accessing via Column and Index:
df['col_name'].iloc[]
Integer-Based Indexing: iloc
.iloc
provides versatile integer-based indexing. It uses zero-based indexing for both rows and columns.
import pandas as pd
data = {'col1': [10, 20, 30], 'col2': [40, 50, 60]}
df = pd.DataFrame(data)
# Access the value at row 1, column 0 (second row, first column)
value = df.iloc[1, 0]
print(f"Value using iloc: {value}") # Output: 20
# Access the value at row 0, column 1 (first row, second column)
value = df.iloc[0, 1]
print(f"Value using iloc: {value}") # Output: 40
.iloc
is efficient, especially for large DataFrames, and offers precise control over cell selection using numerical indices.
iat
and at
for Single-Cell Access
.iat
and .at
offer concise single-cell access. .iat
uses integer-based indexing, while .at
uses label-based indexing (column names).
import pandas as pd
data = {'col1': [10, 20, 30], 'col2': [40, 50, 60]}
df = pd.DataFrame(data)
# Get the value at row 1, column 0 using iat
value = df.iat[1, 0]
print(f"Value using iat: {value}") # Output: 20
# Get the value at row 0, column 'col2' using at
value = df.at[0, 'col2']
print(f"Value using at: {value}") # Output: 40
These methods are faster for single-cell access than .iloc
because they return a scalar value, not a Series or DataFrame.
Accessing via Column and Index: df['col_name'].iloc[]
This approach selects a column then accesses a specific row within that column’s underlying NumPy array. It’s less flexible and efficient than the previous methods.
import pandas as pd
data = {'col1': [10, 20, 30], 'col2': [40, 50, 60]}
df = pd.DataFrame(data)
# Access the value at row 1 of 'col1'
value = df['col1'].iloc[1] #More efficient than .values[]
print(f"Value using iloc on column: {value}") # Output: 20
While this might seem convenient, using .iloc
directly on the column selection is generally preferred for efficiency and clarity.
In summary, .iloc
, .iat
, and .at
provide efficient and readable ways to access DataFrame cells. The column-based approach using .iloc
on the selected column is generally preferred over using .values[]
. Choose the method best suited to your needs and coding style.