How can I retrieve a specific cell value from a Pandas DataFrame?

How can I retrieve a specific cell value from a Pandas DataFrame?

Pandas DataFrame is a powerful data structure that allows for efficient manipulation and analysis of data. One common task when working with a DataFrame is retrieving a specific value from a particular cell. To do this, one can use the iloc method and specify the row and column index of the desired cell. Alternatively, the loc method can be used to retrieve the value based on the labeled index of the row and column. Both methods provide a simple and efficient way to retrieve a specific cell value from a Pandas DataFrame.

Get Cell Value from Pandas DataFrame


You can use the following syntax to get a cell value from a pandas DataFrame:

#iloc method
df.iloc[0]['column_name']

#at method
df.at[0,'column_name']

#values method
df['column_name'].values[0]

Note that all three methods will return the same value.

The following examples show how to use each of these methods with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

        points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6
5	23	9	5
6	25	9	9
7	29	4	12

Method 1: Get Cell Value Using iloc Function

The following code shows how to use the .iloc function to get various cell values in the pandas DataFrame:

#get value in first row in 'points' column
df.iloc[0]['points']

25

#get value in second row in 'assists' column
df.iloc[1]['assists']

7

Method 2: Get Cell Value Using at Function

The following code shows how to use the .at function to get various cell values in the pandas DataFrame:

#get value in first row in 'points' column
df.at[0, 'points']

25

#get value in second row in 'assists' column
df.at[1, 'assists']

7

Method 3: Get Cell Value Using values Function

The following code shows how to use the .values function to get various cell values in the pandas DataFrame:

#get value in first row in 'points' column
df['points'].values[0] 

25

#get value in second row in 'assists' column
df['assists'].values[1] 

7

Notice that all three methods return the same values.

Cite this article

stats writer (2024). How can I retrieve a specific cell value from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-retrieve-a-specific-cell-value-from-a-pandas-dataframe/

stats writer. "How can I retrieve a specific cell value from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-retrieve-a-specific-cell-value-from-a-pandas-dataframe/.

stats writer. "How can I retrieve a specific cell value from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-retrieve-a-specific-cell-value-from-a-pandas-dataframe/.

stats writer (2024) 'How can I retrieve a specific cell value from a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-retrieve-a-specific-cell-value-from-a-pandas-dataframe/.

[1] stats writer, "How can I retrieve a specific cell value from a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I retrieve a specific cell value from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top