How to Get A Value From a Cell in a Pandas DataFrame

5/5 - (2 votes)

If you are a data analyst or work with a lot of data, you might have come across the Pandas library for data manipulation. In this tutorial, we will examine how we can get a value of a cell from a Pandas DataFrame.

How to Get A Value From a Cell of a Pandas DataFrame

Related Tutorial: 5 Minutes to Pandas

There are 5 ways to extract value from a cell of a Pandas DataFrame

  1. Extract data using iloc or indexing
  2. Extract data using iat
  3. Extract data using loc
  4. Extract data using at
  5. Extract data using data_frame.values[]

Loading the Dataset

We will examining the above methods by loading a sample dataset. I have used the California housing dataset that you can download from this link

import pandas as pd
data = pd.read_csv('sample_data/california_housing_test.csv')

Let us see the columns of the dataset

data.head(5)

Output

We can see the first 5 rows of the dataset. The dataset has 9 columns. We will now examine the 5 different methods to extract the 2nd row value of the latitude column 

Method 1: Extract Data using iloc or Indexing

We can use normal indexing the extract the value. 

data.iloc[1]['latitude']

Since the indexing starts from 0, the 1st index is used to get the contents for the 2nd row. Once we extract the row, we can extract any column value that we want. In our case we wanted to get the latitude value. We get the following output.

Output

34.26

Method 2: Extract Data using iat

We will not look at another method to extract the latitude value from the 2nd row.

We can call the iat method of the pandas dataframe to get the cell value. The iat value is called using the row index and column index as an argument. In our example latitude is the 1st column index and for the 2nd row we will use the 1st Index. 

data.iat[1,1]

     Output

34.26

Method 3: Extract Data using loc

We can use the loc method to get the value. The loc method unlike the ioc method can be used by passing in a string as an argument if the index values are strings. In our example since all indexes are numerical values, we can do the following

data.loc[1]['latitude']

      Output

34.26

Method 4: Extract Data using at

The fourth way to extract a value from the cell is using the at method. The at method takes in the row index as an argument and the column name as the second argument. 

data.at[1,'latitude']

   Output

34.26

Method 5: Extract Data using data_frame.values[]

The last method to extract the value from a specific cell is to first convert the frame into a series by using the column name we are interested in getting the value from and then converting the series into a list using the values property. We can then use normal row indexing to get the value from a specific row.

data['latitude'].values[1]

 Output

34.26

👉 Recommended Tutorial: Python Find Longest String in a DataFrame Column

Summary

In this blog post, we saw 5 methods to extract the value from a pandas dataframe. Depending on the use case, we can use any of the above 5 methods to get a value from a cell.

Programming Humor

💡 Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.

~~~

  • Question: Why do Java programmers wear glasses?
  • Answer: Because they cannot C# …!

Feel free to check out our blog article with more coding jokes. 😉