In this tutorial, we will look at how to get (or access) the value of a specific cell in a Pandas dataframe with the help of some examples.
How to get the cell value in Pandas dataframe?
Use the Pandas dataframe iat
property to get the cell value of a dataframe using its row and column indices (integer positions). Alternatively, if you want to access the value of a cell using its row and column labels, use the at
property.
The following is the syntax –
# get cell value using row and column indices (integer positions) df.iat[row_position, column_position] # get cell value using row and column labels df.at[row_label, column_label]
It returns the single cell value for the given row/column pair.
Note that we use the iat
or the at
property to specifically access a single value for the given row and column indices (or labels). You can also use the iloc
and the loc
property to access the value of a single (and multiple cells) in a Pandas dataframe.
Examples
Let’s now look at some examples of using the above syntax to access the value of a cell in a Pandas dataframe.
First, we will create a dataframe that we will use throughout this tutorial.
import pandas as pd # employee data data = { "Name": ["Jim", "Dwight", "Angela", "Tobi"], "Age": [26, 28, 27, 32], "Department": ["Sales", "Sales", "Accounting", "HR"] } # create pandas dataframe df = pd.DataFrame(data) # display the dataframe df
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Here, we created a dataframe containing information about some employees in an office. The dataframe has 4 rows and 3 columns (“Name”, “Age”, and “Department”).
Example 1 – Access dataframe cell value using iat
property
Let’s get the department of the employee “Dwight”.
To access a cell value using the iat
property, you need to provide its row and column indices. Note that rows and columns in a Pandas dataframe are indexed starting from 0 by default.
# get cell value using row and column indices (integer position) df.iat[1, 2]
Output:
'Sales'
We get the value for the cell with row index 1 and column index 2 (which represents the department of the employee “Dwight”).
Example 2 – Access the dataframe cell value using the at
property
Let’s do the same exercise as above but use the row and column labels with the at
property this time.
The column label is the column name itself and since the above dataframe does not have explicitly defined row labels, we will use its row indices as the row label.
# get cell value using row and column labels df.at[1, "Department"]
Output:
'Sales'
We get the “Department” value in the row 1
(which represents the department of the employee “Dwight”).
Using iloc
and loc
to access a cell value in Pandas dataframe
The iloc
and loc
properties of a Pandas dataframe are used to access a group of rows and columns but you can also use them to access the value for a single cell.
Let’s do the same exercise as above.
# get cell value using row and column indices (integer position) df.iloc[1, 2]
Output:
'Sales'
Here, we get the value of the cell represented by row index 1 and column index 2 using the iloc
property.
# get cell value using row and column labels df.loc[1, "Department"]
Output:
'Sales'
Here, we get the value of the cell represented by the row label 1 and the column label “Department” using the loc
property.
Summary
In this tutorial, we looked at how to access the value of a cell in a Pandas dataframe. If you only want the value of a single cell use the iat
or the at
dataframe property.
- Use the
iat
property to access the cell value using its row and column index. - Use the
at
property to access the cell value using its row and column labels.
You can also use the iloc
and the loc
property but they are generally used to get the value of a group of rows and columns.
You might also be interested in –
- Pandas – Set Value of Specific Cell in DataFrame
- Pandas – Get DataFrame Size (Count Cells)
- Pandas – Create DataFrame Copy
- Pandas – Add Column From Another Dataframe
- Pandas – Add an Empty Column to a DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.