Get Cell Value From Pandas DataFrame



Pandas is a popular data analysis library in Python. It provides many powerful functions to manipulate and analyze data. One common task when working with data is to access individual cell values in a DataFrame.

In this article, we’ll explore different ways to get cell values from a Pandas DataFrame.

Using iloc

The iloc method is used to retrieve values from a DataFrame by row and column index.
The syntax for iloc is df.iloc[row_index, col_index].

For example, if we want to retrieve the value in the second row and third column of a DataFrame df,


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] # row index starts from 0, column index starts from 0
print(value) # Output: 'London'

In this example, we use iloc to retrieve the value in the second row and third column of the DataFrame df.
The result is ‘London’, which is the value in the ‘city’ column of the second row.

Using loc

The loc method is used to retrieve values from a DataFrame by row and column labels.
The syntax for loc is df.loc[row_label, col_label].
For example, if we want to retrieve the value in the second row and ‘city’ column of a DataFrame df,
we can use the following code:


value = df.loc[1, 'city']
print(value) # Output: 'London'

In this example, we use loc to retrieve the value in the second row and ‘city’ column of the DataFrame df.
The result is ‘London’, which is the value in the ‘city’ column of the second row.

Method 3: Using the at() Method

The at() method is another way to get a cell value from a Pandas DataFrame. It takes in the row label and column label as its arguments.

import pandas as pd

data = {'name': ['John', 'Sarah', 'Mike'], 
        'age': [25, 30, 35], 
        'salary': [50000, 60000, 70000]}
df = pd.DataFrame(data, columns=['name', 'age', 'salary'])

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

Output:

Sarah

Method 4: Using the iat() Method

The iat() method is similar to the at() method, but instead of using row and column labels, it uses integer positions to identify the cell.

import pandas as pd

data = {'name': ['John', 'Sarah', 'Mike'], 
        'age': [25, 30, 35], 
        'salary': [50000, 60000, 70000]}
df = pd.DataFrame(data, columns=['name', 'age', 'salary'])

print(df.iat[1, 0])

Output:

Sarah

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

We can also use the loc[] and iloc[] methods to slice a DataFrame and get a cell value. loc[] uses labels to identify rows and columns, while iloc[] uses integer positions.

import pandas as pd

data = {'name': ['John', 'Sarah', 'Mike'], 
        'age': [25, 30, 35], 
        'salary': [50000, 60000, 70000]}
df = pd.DataFrame(data, columns=['name', 'age', 'salary'])

print(df.loc[1, 'name'])
print(df.iloc[1, 0])

Output:

Sarah
Sarah

Last Updated on May 12, 2023 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs