Pandas DataFrame head()

This tutorial will teach us about the head() function in pandas DataFrame.

The Head function is basically used when the DataFrame is huge / way too big to analyze the rows for instance.

We use the head() function to get some rows when working with it. By default head() function returns 5 rows which can later be adjusted according to the user’s needs.

Pandas DataFrame head()

import pandas as pd

table = pd.DataFrame({
    'Name' : ['st1','st2','st3','st4','st5','st6','st7','st8','st9','st10'],
    'Marks' : [56,54,3,23,87,87,97,65,76,75]
})

Here we have a DataFrame with students and their marks. So if we want to access the first five rows of this we will use the head function.

print(table.head())

Output:

  Name Marks
0 st1  56
1 st2  54
2 st3  3
3 st4  23
4 st5  87

So the output is the first five rows.

If we want to get any other number of rows we can get that by specifying the row number in the head function.

print(table.head(8))

Output:

  Name Marks
0 st1  56
1 st2  54
2 st3  3
3 st4  23
4 st5  87
5 st6  87
6 st7  97
7 st8  65

Therefore we have learned about the head function in pandas dataframe.

Leave a Reply

Your email address will not be published. Required fields are marked *