Pandas DataFrame head() Method in Python

Pandas DataFrame head() method returns the top n rows of a DataFrame or Series where n is a user input value. It is helpful for quickly testing if your object has the right type of data in it. For negative values of n, the head() function returns all rows except the last n rows, equivalent to df[:-n].

Syntax

DataFrame.head(n=5) (n=5 is default we can set any value)

Parameters

The head() method in python contains only one parameter, n. It is an optional parameter. By setting it, we fix the number of rows we want from the DataFrame.

Return Value

The head() function returns n rows from the DataFrame.

Example 1

import pandas as pd
import numpy as np

data_set = pd.DataFrame(
           {'Name': ['Rohit', 'Mohit', 'Shubh', 'Pranav', 'Shivam', 'Prince'],
           'Class': ['10', '09', '11', '12', '05', '07']})
print(data_set.head(5))

Output

     Name   Class
0   Rohit    10
1   Mohit    09
2   Shubh   11
3  Pranav   12
4  Shivam   05

Here we can see that we have created a DataFrame data_set, which holds the values as names of 6 students and their respective classes in which they study.

Suppose we want to extract the data of only the top 5 students and not all the students. When this problem arises, we can use the head() method, defined in the Pandas library, to extract the top n rows of a dataset.

Example 2

Write a program to use the head() function when the DataFrame consists of 5 columns.

import pandas as pd
import numpy as np
data_frame = pd.DataFrame(
  {'Name': ['Rohit', 'Mohit', 'Shubh', 'Pranav', 'Shivam'],
 'Class': ['10', '09', '11', '12', '05'], 
  'Roll no': ['25', '37', '48', '47', '46'], 
  'Fav Subject': ['C++', 'Python', 'Kotlin', 'C', 'Java'], 
'Favourite Sports': ['Football', 'Basketball', 'Hockey', 'Cricket', 'Handball']
 })
print("DataFrame::\n")
print(data_frame)
print("\n")
print("Top 3 students::")
print("\n")
print(data_frame.head(3))

Output

DataFrame::

     Name Class Roll no Fav Subject Favourite Sports
0   Rohit    10      25         C++         Football
1   Mohit    09      37      Python       Basketball
2   Shubh    11      48      Kotlin           Hockey
3  Pranav    12      47           C          Cricket
4  Shivam    05      46        Java         Handball

Top 3 students::

    Name Class Roll no Fav Subject Favourite Sports
0  Rohit    10      25         C++         Football
1  Mohit    09      37      Python       Basketball
2  Shubh    11      48      Kotlin           Hockey

Here we can see five columns in the DataFrame, and with the help of the head function, we are showing the data of the top 3 students.

Example 3

If you don’t pass any argument to the DataFrame head() function, you will get the default first five rows in return.

import pandas as pd
import numpy as np

data_frame = pd.DataFrame(
 {'Name': ['Rohit', 'Mohit', 'Shubh', 'Pranav', 'Shivam'],
 'Class': ['10', '09', '11', '12', '05'], 
 'Roll no': ['25', '37', '48', '47', '46'], 
 'Fav Subject': ['C++', 'Python', 'Kotlin', 'C', 'Java'], 
 'Favourite Sports': ['Football', 'Basketball', 'Hockey', 'Cricket', 'Handball']
})

print("DataFrame::\n")
print(data_frame)
print("\n")
print(data_frame.head())

Output

DataFrame::

     Name Class Roll no Fav Subject Favourite Sports
0   Rohit    10      25         C++         Football
1   Mohit    09      37      Python       Basketball
2   Shubh    11      48      Kotlin           Hockey
3  Pranav    12      47           C          Cricket
4  Shivam    05      46        Java         Handball


     Name Class Roll no Fav Subject Favourite Sports
0   Rohit    10      25         C++         Football
1   Mohit    09      37      Python       Basketball
2   Shubh    11      48      Kotlin           Hockey
3  Pranav    12      47           C          Cricket
4  Shivam    05      46        Java         Handball

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.