Skip to Content

Pandas – Get Value of a Cell in Dataframe

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?

get value of a cell 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:

dataframe with employee information

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.