Javaexercise.com

How To Get A Value From A Cell Of A Pandas DataFrame?

A DataFrame is the primary data structure of the Pandas library and is commonly used for storing and working with tabular data. A common operation that could be performed on such data is to get the value of a cell i.e. the row value of a given column in order to obtain more insight into the information.

To start working with Pandas, we first need to import it in Python code:

Python 3 Code :

import pandas as pd

Running Example

Let us understand this operation with the help of an example. Consider the following DataFrame containing 3 students with names A, B and C and their corresponding marks (out of 10) for two subjects, Mathematics and Physics.

Get A Value From A Cell Of A Pandas DataFrame

Code snippet for generating the above DataFrame : 

Python 3 Code : 

import pandas as pd

# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}

# DataFrame for the dictionary
df = pd.DataFrame(data)

# Printing the DataFrame
print(df)

Here, ‘data’ is a dictionary we created to initialize the DataFrame. For this, we use the DataFrame() function of the Pandas library which takes the dictionary as an argument and returns the required DataFrame.

Now let us say that for some reason, we want to get the second row’s value of a given column, say Physics. The resulting output will look as follows : 

9

Let us look at different ways of performing this operation on a given DataFrame : 

Get A Value From A Cell Of A Pandas DataFrame Using simple indexing

In this method, we use simple indexing to get the value in a particular cell of a given Pandas dataframe.

Let the column of interest be the one with the label as Physics here. We do this by first referencing the column using its label, by df[‘Physics’] and then using simple indexing thereafter to obtain the second element in the list returned by df[‘Physics’] i.e by using df[‘Physics’][1].

Let us look at the code and corresponding output for this method.

Python 3 Code :

import pandas as pd

# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}

# DataFrame for the dictionary
df = pd.DataFrame(data)

# Get the value in the second row of column labeled as Physics
value = df['Physics'][1]

# Print the value
print(value)

Output : 

9

Get A Value From A Cell Of A Pandas DataFrame Using the DataFrame.loc property

In this method, we use the DataFrame.loc property to get the value in a particular cell of a given Pandas dataframe.

Let the column of interest be the one with the label as Physics here. We do this by passing the first argument as the index of the row coordinate of the desired cell, here 1, and the second argument as the label of the column coordinate for this particular cell, here ‘Physics’.

Let us look at the code and corresponding output for this method.

Python 3 Code :

import pandas as pd

# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}

# DataFrame for the dictionary
df = pd.DataFrame(data)

# Get the value in the second row of column labeled as Physics
value = df.loc[1, 'Physics']

# Print the value
print(value)

Output : 

9

Get A Value From A Cell Of A Pandas DataFrame Using the DataFrame.iloc property

In this method, we use the DataFrame.iloc property to get the value in a particular cell of a given Pandas dataframe.

Let the column of interest be the one with the label as Physics here. We do this by passing the first argument as the index of the row coordinate of the desired cell, here 1, and the second argument as the index of the required column.

We get this index by using the dataframe.columns.get_loc() function. The desired column label is passed as a parameter to this function.

Let us look at the code and corresponding output for this method.

Python 3 Code :

import pandas as pd

# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}

# DataFrame for the dictionary
df = pd.DataFrame(data)

# Get the value in the second row of column labeled as Physics
value = df.iloc[1, df.columns.get_loc('Physics')]

# Print the value
print(value)

Output : 

9

Get A Value From A Cell Of A Pandas DataFrame Using the DataFrame.at property

In this method, we use the DataFrame.at property to get the value in a particular cell of a given Pandas dataframe. Let the column of interest be the one with the label as Physics here.

We do this by passing the first argument as the index of the row coordinate of the desired cell, here 1, and the second argument as the label of the column coordinate for this particular cell, here ‘Physics’.

For this application, the dataframe.at property works similar to the dataframe.loc property.

Let us look at the code and corresponding output for this method.

Python 3 Code :

import pandas as pd

# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}

# DataFrame for the dictionary
df = pd.DataFrame(data)

# Get the value in the second row of column labeled as Physics
value = df.at[2, 'Physics']

# Print the value
print(value)

Output : 

9

Get A Value From A Cell Of A Pandas DataFrame Using the DataFrame.iat property

In this method, we use the DataFrame.iat property to get the value in a particular cell of a given Pandas dataframe.

Let the column of interest be the one with the label as Physics here. We do this by passing the first argument as the index of the row coordinate of the desired cell, here 1, and the second argument as the index of the required column.

We get this index by using the dataframe.columns.get_loc() function. The desired column label is passed as a parameter to this function.

For this application, the dataframe.iat property works similar to the dataframe.iloc property.

Let us look at the code and corresponding output for this method.

Python 3 Code :

import pandas as pd

# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}

# DataFrame for the dictionary
df = pd.DataFrame(data)

# Get the value in the second row of column labeled as Physics
value = df.iat[1, df.columns.get_loc('Physics')]

# Print the value
print(value)

Output : 

9

Conclusion

In this topic, we have learned to get the value of a cell i.e. the row value of a given column of an existing DataFrame, following a running example of test scores of students in different subjects, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions.