Pandas Dataframe is a two-dimensional array used to store values in rows and columns format. You may need to access the value of a cell to perform some operations on it.
You can get the value of a cell from a pandas dataframe using df.iat[0,0]
.
In this tutorial, you’ll learn how to get the value of a cell from a pandas dataframe.
If you’re in Hurry
You can use the below code snippet to get a specific cell value.
It’ll return the value of the cell with row index 0
and column index 0
.
Snippet
df.iat[0,0]
Output
Keyboard
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to get the value of a specific cell of a pandas dataframe.
To set the value of a cell from a dataframe, read How to Set Value of A Cell From Pandas Dataframe.
Table of Contents
Sample Dataframe
This is the sample dataframe used throughout the tutorial.
Snippet
import pandas as pd
data = {"Product_Name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers","Headset"],
"Unit_Price":[500,200, 5000.235, 10000.550, 10000.550, 250.50,None],
"No_Of_Units":[5,5, 10, 20, 20, 8,pd.NaT],
"Available_Quantity":[5,6,10,"Not Available","Not Available", pd.NaT,pd.NaT],
"Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT],
"Remarks":[pd.NaT,pd.NaT,pd.NaT,pd.NaT,pd.NaT,pd.NaT,pd.NaT]
}
df = pd.DataFrame(data)
df = df.astype({"Unit_Price": float})
df
DataFrame Will Look Like
Product_Name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | Remarks | |
---|---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5 | 5 | 11/5/2021 | NaT |
1 | Mouse | 200.000 | 5 | 6 | 4/23/2021 | NaT |
2 | Monitor | 5000.235 | 10 | 10 | 08/21/2021 | NaT |
3 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 | NaT |
4 | CPU | 10000.550 | 20 | Not Available | 09/18/2021 | NaT |
5 | Speakers | 250.500 | 8 | NaT | 01/05/2021 | NaT |
6 | Headset | NaN | NaT | NaT | NaT | NaT |
Now, you’ll learn the different ways to get value from a specific cell of the dataframe.
Get Cell Value Using iat
You can get cell value using iat
property of the dataframe.
iat attribute accepts row index and column index position to fetch the value at that specific position. It raises IndexError
when the index passed is out of bounds.
Use the below snippet to get the value of the cell at position [0,0]
.
Snippet
df.iat[0,0]
Output
Keyboard
This is how you can access the cell value of a dataframe using iat
property of the dataframe.
Get Cell Value using At
You can get cell value using at
property of the dataframe. Unlike iat
, at
also accepts the column name as the positional parameter.
at attribute accepts row index and column index or column name to fetch the value at that specific position. It raises IndexError
when the index passed is out of bounds.
Use the below snippet to get the value of the cell at position 0 for the column Product_Name.
Snippet
df.at[1,'Product_Name']
Output
Mouse
This is how you can use the at attribute to get the value of a cell in the dataframe.
Get Cell Value Using iloc
iloc attribute is integer-based indexing for selecting a value from the dataframe.
You can get cell value using iloc
property of the dataframe.
You can pass the row index and the column index or name to get the value at that specific position.
It raises IndexError
when the index passed is out of bounds.
Use the below snippet to get the value of the cell at row index 2
and column name No_Of_Units
.
Snippet
df.iloc[2]['No_Of_Units']
Output
10
This is how you can select the value of a cell from the pandas dataframe.
Get Cell Value By Column Name
In this section, you’ll learn how to get cell values by using the column name.
You can get cell value by column name by using the values[] attribute.
First, select the specific column by using its name using df[‘Product_Name’] and get the value of a specific cell using values[0]
as shown below.
Snippet
df['Product_Name'].values[0]
Output
'Keyboard'
This is how you can get the cell value of a dataframe by using the column name.
Get Cell Value Based on Condition
In this section, you’ll learn how to get cell value based on a condition.
You can get cell value based on condition by passing the condition to the loc attribute.
To select the Product_Name which has a price greater than or equal to 1000, you can use the below snippet.
tolist()
method converts values from the cells to list and displays them.
Snippet
df.loc[df.Unit_Price >= 1000,'Product_Name'].tolist()
Output
['Monitor', 'CPU', 'CPU']
If you would like to select one cell value based on the condition, you can again use the values property after filtering the dataframe using the condition in the loc
attribute.
Snippet
df.loc[df.Unit_Price >= 1000,'Product_Name'].values[0]
This will return the first product which has a price greater than or equal to 0.
Output
Monitor
This is how you can get the value of a cell based on a specific condition.
Get Cell Value by Row and Column Name
In this section, you’ll learn how to get the value of a cell by using row and column names.
In most cases, the dataframe columns will have names. You can set names for rows using the set_index()
method.
For example, use the below snippet to set the column Product_Name as the row name. Use inplace = True
for making changes in the same dataframe rather than creating a new dataframe.
Snippet
# setting first name as index column
df.set_index("Product_Name", inplace = True)
Once you have a dataframe with names for rows and columns.
Use the below snippet to get No_Of_Units
of the product Keyboard
.
Snippet
df.loc['Keyboard']['No_Of_Units']
You’ll see the output 5 as the number of units of keyboard available is 5
.
Output
5
This is how you can get cell value by row and column name.
Conclusion
To summarize, you’ve learned how to get the cell value of the dataframe using the different available methods. You’ve also learned how to get a specific cell value by using column names or column indexes.
If you have any questions, comment below.
Thank you for this clear explanation. I’m new at python and spend hours trying to find an explanation as clear as this about how to get values from a dataframe.
Hello Peter,
Thank you very much for the feedback.
Educating readers in the best possible way is my motto here. Will keep writing more detailed tutorials like this.
Regards,
Vikram