How to Use Pandas head() Function (With Examples)

The Pandas head() function is used to return the first n rows from a dataframe, where “n” is an integer value. It also allows for the selection of a specific number of rows and columns from the dataframe, and can be used to quickly preview the data before further processing. This function is very useful for quickly exploring a dataset and understanding its basic structure. For example, one might use head() to determine the number of columns and the data types contained in each column of a dataframe.


You can use the head() function to view the first n rows of a pandas DataFrame.

This function uses the following basic syntax:

df.head()

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6
5	23	9	5
6	25	9	9
7	29	4	12

Example 1: View First 5 Rows of DataFrame

By default, the head() function displays the first five rows of a DataFrame:

#view first five rows of DataFrame
df.head()

	points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6

Example 2: View First n Rows of DataFrame

We can use the n argument to view the first n rows of a pandas DataFrame:

#view first three rows of DataFrame
df.head(n=3)

        points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10

Example 3: View First n Rows of Specific Column

The following code shows how to view the first five rows of a specific column in a DataFrame:

#view first five rows of values in 'points' column
df['points'].head()

0    25
1    12
2    15
3    14
4    19
Name: points, dtype: int64

Example 4: View First n Rows of Several Columns

The following code shows how to view the first five rows of several specific columns in a DataFrame:

#view first five rows of values in 'points' and 'assists' columns
df[['points', 'assists']].head()

	points	assists
0	25	5
1	12	7
2	15	7
3	14	9
4	19	12

The following tutorials explain how to perform other common functions in pandas:

x