How to Get Cell Value from Pandas DataFrame?
Last Updated :
06 Dec, 2024
In this article, we will explore various methods to retrieve cell values from a Pandas DataFrame in Python. Pandas provides several functions to access specific cell values, either by label or by position.
Get value from a cell of Dataframe using loc() function
The .loc[] function in Pandas allows you to access a group of rows and columns by labels. It is commonly used when you know the exact row and column labels.
Python
import pandas as pd
# Create DataFrame
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
})
# Accessing cell value using loc[]
print(data['id'].loc[data.index[0]]) # 7058
print(data['name'].loc[data.index[1]]) # jyothika
print(data['subjects'].loc[data.index[3]]) # php/js
Output7058
jyothika
php/js
Using iloc[] for Position-based Access
If you don’t know the index labels or if the DataFrame's index is not numeric, .iloc[] is used to select data by integer position.
Python
import pandas as pd
# Create DataFrame
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
})
# Accessing cell value using iloc[]
print(data['id'].iloc[0]) # 7058
print(data['name'].iloc[0]) # sravan
print(data['subjects'].iloc[0]) # java
Using values[] for Accessing Data as an Array
The .values[] function returns the DataFrame’s data as a NumPy array, allowing direct indexing to retrieve cell values.
Python
# import pandas module
import pandas as pd
# get the cell value using values() function
# in id column and 1 row
print(data['name'].values[0])
# get the cell value using values() function
# in id column and 2 row
print(data['id'].values[1])
# get the cell value using values() function
# in subjects column and 4 row
print(data['subjects'].values[3])
Output:
sravan
7059
php/js
Using at[] for Fast Scalar Access
The .at[] function allows you to access a single value at a specific row and column. This is similar to .loc[], but it is optimized for quick access to individual cell values.
Python
# import pandas module
import pandas as pd
# get the cell value using at() function
# in id column and 2 row
print(data.at[1, 'id'])
# get the cell value using at() function
# in id column and 4 row
print(data.at[3, 'name'])
# get the cell value using at() function
# in subjects column and 1 row
print(data.at[0, 'subjects'])
Output:
7059
ramya
java
Using iat[] for Fast Position-based Access
The .iat[] function is used for accessing a specific cell by row and column index position. It is similar to .at[] but uses integer positions.
Python
import pandas as pd
# Create DataFrame
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
})
# Accessing cell value using iat[]
print(data.iat[0, 0]) # 7058
print(data.iat[0, 1]) # sravan
print(data.iat[1, 0]) # 7059
Output:
7058
sravan
7059
Each of the methods listed above provides a way to access specific values in a Pandas DataFrame, with different strengths based on your needs:
- .loc[] and .iloc[] are versatile for label-based and position-based indexing.
- .values[] is useful when you need to work directly with the DataFrame’s underlying array.
- .at[] and .iat[] provide optimized access for individual cell retrieval by label or position.