How to Get Cell Value from Pandas DataFrame? - GeeksforGeeks

30 Nov, 2021
In this article, we will discuss how to get the cell value from the pandas dataframe.
This method is used to get the  particular cell data with index function by using the column name
Syntax:
dataframe[‘column_name’].loc[dataframe.index[row_number]]
where,
dataframe is the input dataframe
index is the function to get row_numer of the cell
column_name represents the cell column name
Example: Python program to get particular cell using loc() function
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get the cell value using loc() function# in id column and 1 rowprint(data['id'].loc[data.index[0]]) # get the cell value using loc() function# in id column and 2 rowprint(data['id'].loc[data.index[1]]) # get the cell value using loc() function# in id column and 3 rowprint(data['id'].loc[data.index[2]]) # get the cell value using loc() function# in id column and 4 rowprint(data['id'].loc[data.index[3]]) # get the cell value using loc() function# in name column and 1 rowprint(data['name'].loc[data.index[0]]) # get the cell value using loc() function# in name column and 2 rowprint(data['name'].loc[data.index[1]]) # get the cell value using loc() function# in subjects column and 3 rowprint(data['subjects'].loc[data.index[2]]) # get the cell value using loc() function# in subjects column and 4 rowprint(data['subjects'].loc[data.index[3]])
Output:
7058
7059
7072
7054
sravan
jyothika
html/php
php/js
This method is used to access the cell data by using column names and row index numbers.
Syntax:
dataframe[‘column_name’].iloc[row_number]
where
dataframe is the input dataframe
row_numer represents the cell row position of the cell
column_name represents the cell column name
Example: Python program to get the cell values using iloc function
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get the cell value using iloc() function# in id column and 1 rowprint(data['id'].iloc[0]) # get the cell value using iloc() function# in id column and 2 rowprint(data['id'].iloc[1]) # get the cell value using iloc() function# in name column and 1 rowprint(data['name'].iloc[0]) # get the cell value using iloc() function# in id column and 4 rowprint(data['name'].iloc[3]) # get the cell value using iloc() function# in subjects column and 1 rowprint(data['subjects'].iloc[0]) # get the cell value using iloc() function# in subjects column and 4 rowprint(data['subjects'].iloc[3])
Output:
7058
7059
sravan
ramya
java
php/js
This will return the cell value by providing the row_number and the column_name.
Syntax:
dataframe[‘column_name’].values[row_number]
where,
dataframe is the input dataframe
row_numer represents the cell row position of the cell
column_name represents the cell column name
Example: Python code to get cell value from the dataframe
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get the cell value using values() function# in id column and 1 rowprint(data['name'].values[0]) # get the cell value using values() function# in id column and 2 rowprint(data['id'].values[1]) # get the cell value using values() function# in name column and 1 rowprint(data['name'].values[0]) # get the cell value using values() function# in id column and 4 rowprint(data['name'].values[3]) # get the cell value using values() function# in subjects column and 1 rowprint(data['subjects'].values[0]) # get the cell value using values() function# in subjects column and 4 rowprint(data['subjects'].values[3])
Output:
sravan
7059
sravan
ramya
java
php/js
This function will return the cell value by taking the row number and column name.
Syntax:
dataframe.at[row_index, column_name]
where
dataframe is the input dataframe
row_index  to get the  row based on the index number given
column_name is the name of the column
Example: Python program to get the cell values by using at[] function
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get the cell value using at() function# in id column and 1 rowprint(data.at[0, 'id']) # get the cell value using at() function# in id column and 2 rowprint(data.at[1, 'id']) # get the cell value using at() function# in name column and 1 rowprint(data.at[0, 'name']) # get the cell value using at() function# in id column and 4 rowprint(data.at[3, 'name']) # get the cell value using at() function# in subjects column and 1 rowprint(data.at[0, 'subjects']) # get the cell value using values() function# in subjects column and 4 rowprint(data.at[3, 'subjects'])
Output:
7058
7059
sravan
ramya
java
php/js
This function takes row and column index to display cell value in the dataframe.
Syntax:
dataframe.iat[row_index, column_index]
where,
dataframe is the input dataframe
row_index is the row number
column_index is the column number
Example: Python program to get the cell values using iat function
Python3
# import pandas moduleimport pandas as pd # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']}) # get the cell value using iat() function# in id column and 1 rowprint(data.iat[0, 0]) # get the cell value using iat() function# in name column and 1 rowprint(data.iat[0, 1]) # get the cell value using iat() function# in id column and 2 rowprint(data.iat[1, 0])
Output:
7058
sravan
7059
kapoorsagar226
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python