How do i show the first row in python?

  1. HowTo
  2. Python Pandas Howtos
  3. Get the First Row of Dataframe Pandas

Created: January-16, 2021 | Updated: February-21, 2021

  1. Get the First Row of a Pandas DataFrame Using pandas.DataFrame.iloc Property
  2. Get the First Row From a Pandas DataFrame Based on Specified Condition

This tutorial explains how we can get the first row from a Pandas DataFrame using the pandas.DataFrame.iloc property and pandas.DataFrame.head[] method.

We will use the DataFrame in the example below to explain how we can get the first row from a Pandas 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],
}]

print[df]

Output:

  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.

Related Article - Pandas DataFrame Row

  • Get the Row Count of a Pandas DataFrame
  • Randomly Shuffle DataFrame Rows in Pandas
  • Filter Dataframe Rows Based on Column Values in Pandas
  • Iterate Through Rows of a DataFrame in Pandas
  • How do you get the first row in Python?

    Method 1: Using iloc[] This method is used to access the row by using row numbers. We can get the first row by using 0 indexes.

    How do I show the first 10 rows in Python?

    Use pandas. DataFrame. head[n] to get the first n rows of the DataFrame. It takes one optional argument n [number of rows you want to get from the start].

    How do I show the first 5 rows in Python?

    Pandas DataFrame – Get First N Rows – head[] DataFrame. head[] . You can pass an optional integer that represents the first N rows. If you do not pass any number, it returns the first 5 rows.

    What is first [] in Python?

    The first[] method returns the first n rows, based on the specified value. The index have to be dates for this method to work as expected.

    Chủ Đề