Created: January-16, 2021 | Updated: February-21, 2021 This tutorial explains how we can get the first row from a Pandas DataFrame using the We will use the DataFrame in the example below to explain how we can get the first row from a Pandas DataFrame. Output:pandas.DataFrame.iloc
Propertypandas.DataFrame.iloc
property and pandas.DataFrame.head[]
method.import pandas as pd
df = pd.DataFrame[{
'C_1': ["A","B","C","D"],
'C_2': [40,34,38,45],
'C_3': [430, 980, 200, 350],
}]
print[df]
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 200
3 D 45 350
Get the First Row of a Pandas DataFrame Using pandas.DataFrame.iloc
Property
import pandas as pd
df = pd.DataFrame[{
'C_1': ["A","B","C","D"],
'C_2': [40,34,38,45],
'C_3': [430, 980, 200, 350],
}]
row_1=df.iloc[0]
print["The DataFrame is:"]
print[df,"\n"]
print["The First Row of the DataFrame is:"]
print[row_1]
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 200
3 D 45 350
The First Row of the DataFrame is:
C_1 A
C_2 40
C_3 430
Name: 0, dtype: object
It displays the first row of the DataFrame df
. To select the first row, we use the default index of the first row i.e. 0
with the iloc
property of the DataFrame.
Get the First Row From a Pandas DataFrame Using the pandas.DataFrame.head[]
Method
The pandas.DataFrame.head[]
method returns a DataFrame with topmost 5 rows of the
DataFrame. We can also pass a number as an argument to the pandas.DataFrame.head[]
method representing the number of topmost rows to be selected. We can pass 1 as an argument to the pandas.DataFrame.head[]
method to only select the first row of the DataFrame.
import pandas as pd
df = pd.DataFrame[{
'C_1': ["A","B","C","D"],
'C_2': [40,34,38,45],
'C_3': [430, 980, 200, 350],
}]
row_1=df.head[1]
print["The DataFrame is:"]
print[df,"\n"]
print["The First Row of the DataFrame is:"]
print[row_1]
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 200
3 D 45 350
The First Row of the DataFrame is:
C_1 C_2 C_3
0 A 40 430
Get the First Row From a Pandas DataFrame Based on Specified Condition
To extract the first row satisfying specified conditions from a DataFrame, we at first filter the rows satisfying specified conditions and then select the first row from the filtered DataFrame using the methods discussed above.
import pandas as pd
df = pd.DataFrame[{
'C_1': ["A","B","C","D"],
'C_2': [40,34,38,45],
'C_3': [430, 980, 500, 350],
}]
filtered_df=df[[df.C_2 < 40] & [df.C_3 > 450]]
row_1_filtered=filtered_df.head[1]
print["The DataFrame is:"]
print[df,"\n"]
print["The Filtered DataFrame is:"]
print[filtered_df,"\n"]
print["The First Row with C_2 less than 45 and C_3 greater than 450 is:"]
print[row_1_filtered]
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 500
3 D 45 350
The Filtered DataFrame is:
C_1 C_2 C_3
1 B 34 980
2 C 38 500
The First Row with C_2 less than 45 and C_3 greater than 450 is:
C_1 C_2 C_3
1 B 34 980
It will display the first row with the value of column C_2
less than 45 and the value of C_3
column greater than 450.
We can also use the query[]
method to filter the rows from the
DataFrame.
import pandas as pd
df = pd.DataFrame[{
'C_1': ["A","B","C","D"],
'C_2': [40,34,38,45],
'C_3': [430, 980, 500, 350],
}]
filtered_df=df.query['[C_2 < 40] & [C_3 > 450]']
row_1_filtered=filtered_df.head[1]
print["The DataFrame is:"]
print[df,"\n"]
print["The Filtered DataFrame is:"]
print[filtered_df,"\n"]
print["The First Row with C_2 less than 45 and C_3 greater than 450 is:"]
print[row_1_filtered]
Output:
The DataFrame is:
C_1 C_2 C_3
0 A 40 430
1 B 34 980
2 C 38 500
3 D 45 350
The Filtered DataFrame is:
C_1 C_2 C_3
1 B 34 980
2 C 38 500
The First Row with C_2 less than 45 and C_3 greater than 450 is:
C_1 C_2 C_3
1 B 34 980
It will filter all the rows with the value of column C_2
less than 45 and the value of C_3
column greater than 450 using the query[]
method and then select the first row from the filtered_df
using the head[]
method.
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.