Pandas dataframe stores values as rows and columns.
You can get the first row of a column in the pandas dataframe using df.loc[0, ‘Column Name’] statement.
Different methods are available to get the first row of a column in Pandas Dataframe.
If You’re in Hurry…
You can use the loc
attribute with a row axis value as 0
to get the first row of a column in the pandas dataframe.
Code
df.loc[0, 'Column Name’]
You’ll see the first-row value of the column printed.
If You Want to Understand Details, Read on…
While working with the pandas dataframe, you need to get the first value in a given column to know more about the column.
You’ll learn the different methods to get the first row of a given column.
Table of Contents
Sample Dataframe
For demonstration, first, create a dataframe with three columns.
import pandas as pd
import numpy as np
data = {'Column 1': [1,2,np.nan,4,5,np.nan,None],
'Column 2': [2,2,np.nan,4,np.nan,np.nan,None],
'Column 3': [3,2,None,4,5,None,None]
}
df = pd.DataFrame(data,columns=['Column 1','Column 2','Column 3'])
df
Dataframe Will Look Like
Column 1 | Column 2 | Column 3 | |
---|---|---|---|
0 | 1.0 | 2.0 | 3.0 |
1 | 2.0 | 2.0 | 2.0 |
2 | NaN | NaN | NaN |
3 | 4.0 | 4.0 | 4.0 |
4 | 5.0 | NaN | 5.0 |
5 | NaN | NaN | NaN |
6 | NaN | NaN | NaN |
Now, you’ll use this dataframe and select the first row of any given column.
Using loc attribute
In this section, you’ll learn how to select the first row of a column in the pandas dataframe using the loc attribute.
loc
is used to select rows based on their label(s). Hence, you must pass only the row and column labels to get any cell value of a pandas dataframe.
The first row’s index will be 0
if you’ve NOT used any custom row index. If you’ve used any custom index, you can use that index name as the first parameter and the column name as the second parameter to get the first row of any column.
To know the columns available in the dataframe read How to Get Column Names in Pandas.
To know the index of the first-row, you can use the below code snippet.
Get the index of the First row
df.index[0]
Output
0
Now use this index and the column name to select the first row of the specific column.
The code below demonstrates how to select the first-row of the column with the name Column 1.
Code
df.loc[0, 'Column 1']
You’ll see the below output. The first-row value of the Column with Column 1 is printed.
Output
1.0
Using iloc attribute
In this section, you’ll learn how to select the first row of a column in the pandas dataframe using the iloc attribute.
iloc
is used to select rows based on Integer. Hence, you must pass only the row and column index to select any cell value of a pandas dataframe.
The first row’s index will be 0
. Hence, you need to use the 0
in the row index and the appropriate column index in the iloc
attribute.
The below code demonstrates how to select the first row of the first column with the column index 0.
Code
df.iloc[0, 0]
You’ll see the below output. The first row of the first column is printed.
Output
1.0
Using Head() Method
In this section, you’ll learn how to select the first row of a given column using the head() method.
The head()
method prints the first n
rows of the dataframe and is usually used to check if the dataframe has the correct type of data.
To print only the first row of a specific column, you can select the subset of that column using df[[column name]]
and use the head(1)
to print the value.
Code
df[['Column 1']].head(1)
Output
Column 1 | |
---|---|
0 | 1.0 |
Conclusion
To summarise, you’ve learned how to get the first row of a given column in the pandas dataframe.
Also learned, iloc
is used to select based on integer position, and the loc
attribute is used to select based on the row and column labels.