Javaexercise.com

How To Get The First Row Value Of A Given Column In 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 first-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 into 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 The First Row Value Of A Given Column In 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 first-row value of a given column, say Physics. The resulting output will look as follows : 

Output:

7

 

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

Get The First Row Value Of A Given Column Using simple indexing in Pandas

In this method, we use simple indexing to get the first value in a particular column 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 first element in the list returned by df[‘Physics’] i.e by using df[‘Physics’][0].

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 first row of column labeled as Physics
value = df['Physics'][0]

# Print the value
print(value)

Output : 

7

Get The First Row Value Of A Given Column Using the DataFrame.loc property in Pandas

In this method, we use the DataFrame.loc property to get the first value in a particular column 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 0, 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 first row of column labeled as Physics
value = df.loc[0, 'Physics']

# Print the value
print(value)

Output : 

7

Get The First Row Value Of A Given Column Using the DataFrame.iloc property in Pandas

In this method, we use the DataFrame.iloc property to get the first value in a particular column 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 0, 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 first row of column labeled as Physics
value = df.iloc[0, df.columns.get_loc('Physics')]

# Print the value
print(value)

Output : 

7

Get The First Row Value Of A Given Column Using the DataFrame.at property in Pandas

In this method, we use the DataFrame.at property to get the first value in a particular column 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 0, 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 first row of column labeled as Physics
value = df.at[0, 'Physics']

# Print the value
print(value)

Output : 

7

Get The First Row Value Of A Given Column Using the DataFrame.iat property

In this method, we use the DataFrame.iat property to get the first value in a particular column 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 0, 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[0, df.columns.get_loc('Physics')]

# Print the value
print(value)

Output : 

7

Conclusion

In this topic, we have learned to get the first-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 info.javaexercise@gmail.com in case of any suggestions.