In this article, we will discuss different ways to select first row of dataframe in pandas.

Select & print first row of dataframe using iloc[]

Before diving deep into the solution, let’s first have a look at the dataframe’s iloc.

Overview of dataframe iloc[]

Pandas provides a dataframe attribute iloc[] for location based indexing i.e.

dataframe.iloc[row_section, col_section]
dataframe.iloc[row_section]

Arguments if iloc[]

  • row_section: It can be,
    • A row number
    • A list of row numbers
    • A range of row numbers – start:end i.e. from start to end-1.
  • column_section: It can be
    • A column number
    • A column of row numbers
    • A range of column numbers – start:end i.e. from start to end-1.

It selects the subset of dataframe based on the row/column numbers specified in these row & column sections.

Get first row of pandas dataframe as a series

To select the first row of dataframe using iloc[], we can just skip the column section and in row section pass the 1 as row number. It will select the first row i.e. row at index 0,

df.iloc[0]

We got the first row of dataframe as a series object.

Get first row of pandas dataframe as a dataframe

If you want the first row of dataframe as a dataframe object then you can provide the range i.e.[:1], instead of direct number i.e.

df.iloc[:1]

It will select the rows from number 0 to 1 and return the first row of dataframe as a dataframe object.

Complete example:

Let’s see an example, where we will select and print the first row of dataframe using both the specified ways,

import pandas as pd

# List of Tuples
employees = [('Jack',    34, 'Sydney',   5) ,
            ('Shaun',   31, 'Delhi' ,   7) ,
            ('Shachin', 16, 'London',   3) ,
            ('Eva',     41, 'Delhi' ,   4)]

# Create a DataFrame object
df = pd.DataFrame(  employees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

# Select first row of the dataframe as a series
first_row = df.iloc[0]

print("First row Of Dataframe: ")
print(first_row)

# Select first row of the dataframe as a dataframe object
first_row_df = df.iloc[:1]

print("First row Of Dataframe: ")
print(first_row_df)

Output:

Contents of the Dataframe : 
      Name  Age    City  Experience
0     Jack   34  Sydney           5
1    Shaun   31   Delhi           7
2  Shachin   16  London           3
3      Eva   41   Delhi           4

First row Of Dataframe: 
Name            Jack    
Age               34    
City          Sydney    
Experience         5    
Name: 0, dtype: object  

First row Of Dataframe: 
   Name  Age    City  Experience
0  Jack   34  Sydney           5

At, first We selected the first row of dataframe as a series object & printed it. After that we selected the first row as a dataframe and then again printed it.

Select & print first row of dataframe using head()

In Pandas, the dataframe provides a function head(n). It returns the first n rows of dataframe. We can use this head() function to get only the first row of the dataframe,

df.head(1)

It will return the first row of dataframe as a dataframe object.

Let’s see a complete example,

import pandas as pd

# List of Tuples
employees = [('Jack',    34, 'Sydney',   5) ,
            ('Shaun',   31, 'Delhi' ,   7) ,
            ('Shachin', 16, 'London',   3) ,
            ('Eva',     41, 'Delhi' ,   4)]

# Create a DataFrame object
df = pd.DataFrame(  employees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

# Select first row of the dataframe 
first_row = df.head(1)

print("First row Of Dataframe: ")
print(first_row)

Output:

Contents of the Dataframe : 
      Name  Age    City  Experience
0     Jack   34  Sydney           5
1    Shaun   31   Delhi           7
2  Shachin   16  London           3
3      Eva   41   Delhi           4

First row Of Dataframe:
   Name  Age    City  Experience
0  Jack   34  Sydney           5

Using the head() function, we fetched the first row of dataframe as a dataframe and then just printed it.

Get first row of pandas dataframe as list

We can select the first row of dataframe using df.iloc[0]. It will give us a series object and then using the series’s tolist() function, we can get a list containing the contents of first row of dataframe. For example,

import pandas as pd

# List of Tuples
employees = [('Jack',    34, 'Sydney',   5) ,
            ('Shaun',   31, 'Delhi' ,   7) ,
            ('Shachin', 16, 'London',   3) ,
            ('Eva',     41, 'Delhi' ,   4)]

# Create a DataFrame object
df = pd.DataFrame(  employees, 
                    columns=['Name', 'Age', 'City', 'Experience'])

print("Contents of the Dataframe : ")
print(df)

# Select first row as list
first_row = df.iloc[0].tolist()

print("First row Of Dataframe: ")
print(first_row)

Output:

Contents of the Dataframe : 
      Name  Age    City  Experience
0     Jack   34  Sydney           5
1    Shaun   31   Delhi           7
2  Shachin   16  London           3
3      Eva   41   Delhi           4

First row Of Dataframe:
['Jack', 34, 'Sydney', 5]

Summary:

We learned about different ways to get the first row of dataframe.