Sentry Answers>Python>

Get the value of a DataFrame cell in Python Pandas

Get the value of a DataFrame cell in Python Pandas

David Y.

The ProblemJump To Solution

How can I retrieve the value of a single cell in a Python Pandas DataFrame? I want just the value, not a new DataFrame with only one cell.

The Solution

We can retrieve the value of a single DataFrame cell using the property, providing the row and column labels for the element we want to access:

Click to Copy
import pandas # Set up and print the DataFrame data = { "Product": ["Apple", "Orange", "Pear"], "Cost Price": [1, 2, 3], "Selling Price": [2, 3, 4], } df = pandas.DataFrame(data) print(df) print() # will print: # Product Cost Price Selling Price # 0 Apple 1 2 # 1 Orange 2 3 # 2 Pear 3 4 # Retrieve "Pear" df_cell =[2, "Product"] print(df_cell) # will print "Pear"

Note that the value 2 here is being interpreted as a row label, rather than a row number. If we want to access the value of a cell using the indices of its row and column rather than the label, we can use DataFrame.iat:

Click to Copy
# Retrieve "Pear" df_cell = df.iat[2, 0] print(df_cell) # will print "Pear"

We can also use DataFrame.loc in place of, or DataFrame.iloc in place of DataFrame.iat:

Click to Copy
# Retrieve "Pear" df_cell = df.loc[2, "Product"] print(df_cell) # will print "Pear" # Retrieve "Pear" df_cell_by_index = df.iloc[2, 0] print(df_cell_by_index) # will print "Pear"

DataFrame.loc and DataFrame.iloc are more flexible, general accessors, which can both be used to select arbitrarily sized portions of a DataFrame. and DataFrame.iat, on the other hand, are both optimized for retrieving the value of a single cell, so the versions of our code using one of these functions will run faster. This will be especially noticeable with large DataFrames.

  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Sentry BlogLogging in Python: A Developer’s Guide
  • logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.


Loved by over 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.