PythonPandas.com

Get Cell Value From Pandas DataFrame



Pandas is one of the most widely used Python libraries for data manipulation and analysis. A common operation when working with a DataFrame is retrieving individual cell values. For example, extracting a single record or checking a specific field.

In this article, we’ll cover multiple ways to get cell values from a Pandas DataFrame using different methods.

Method 1: Using iloc[] (Index-Based Access)

The iloc[] method retrieves a value using the integer index of rows and columns.
It follows zero-based indexing, meaning the first row and first column start from index 0.

Syntax:

df.iloc[row_index, column_index]

Example:

import pandas as pd

data = {
    'name': ['John', 'Sarah', 'James'],
    'age': [25, 30, 35],
    'city': ['New York', 'London', 'Paris']
}

df = pd.DataFrame(data)
value = df.iloc[1, 2]  # second row, third column
print(value)

Output:

London

Here, iloc[1, 2] returns the cell in the second row and third column — ‘London’.

Method 2: Using loc[] (Label-Based Access)

The loc[] method retrieves values using labels (names) of rows and columns instead of numeric indexes.

Syntax:

df.loc[row_label, column_label]

Example:

import pandas as pd

data = {
    'name': ['John', 'Sarah', 'James'],
    'age': [25, 30, 35],
    'city': ['New York', 'London', 'Paris']
}

df = pd.DataFrame(data)
value = df.loc[1, 'city']
print(value)

Output:

London

loc[1, ‘city’] directly accesses the cell value using row label = 1 and column name = ‘city’.

Method 3: Using the at[] Method (Fast Label-Based Access)

The at[] method is similar to loc[] but optimized for accessing a single cell quickly.
It takes row label and column label as arguments and is faster than loc[] when dealing with individual values.

Syntax:

df.at[row_label, column_label]

Example:

import pandas as pd

data = {
    'name': ['John', 'Sarah', 'Mike'],
    'age': [25, 30, 35],
    'salary': [50000, 60000, 70000]
}

df = pd.DataFrame(data)

print(df.at[1, 'name'])

Output:

Sarah

The at[] method provides faster access than loc[] when retrieving a single value.

Method 4: Using the iat[] Method (Fast Index-Based Access)

The iat[] method works like iloc[] but is optimized for fast access of a single scalar value using integer indexes.

Syntax:

df.iat[row_index, column_index]

Example:

import pandas as pd

data = {
    'name': ['John', 'Sarah', 'Mike'],
    'age': [25, 30, 35],
    'salary': [50000, 60000, 70000]
}

df = pd.DataFrame(data)

print(df.iat[1, 0])

Output:

Sarah

Similar to iloc[1, 0], but iat[] is optimized for single-value access and is slightly faster.

Method 5: Using loc[] and iloc[] for Slicing

Both loc[] and iloc[] can also be used to extract subsets (or slices) of data.
You can use them to get single cells or ranges of rows and columns.

Example:

import pandas as pd

data = {
    'name': ['John', 'Sarah', 'Mike'],
    'age': [25, 30, 35],
    'salary': [50000, 60000, 70000]
}

df = pd.DataFrame(data)

# Using loc
print(df.loc[1, 'name'])

# Using iloc
print(df.iloc[1, 0])

Output:

 Sarah Sarah 

Both statements return ‘Sarah’, demonstrating that label-based (loc) and index-based (iloc) slicing can achieve the same result.

Frequently Asked Questions

Which method is the fastest to get a single cell value in Pandas?
The iat[] and at[] methods are the fastest because they are optimized for single-cell access.
What is the difference between loc and iloc?

loc[] is label-based and uses row/column names, while iloc[] is integer-based and uses positional indexes.

Can I modify a cell value using these methods?

Yes. You can assign a new value directly, e.g. df.at[1, 'age'] = 40 or df.iat[0, 1] = 45.

Which method should I use for accessing multiple cells or ranges?

Use loc[] or iloc[] for accessing ranges or multiple cells since they support slicing.

Related Post