Open In App

Pandas Dataframe/Series.head() method - Python

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The head() method structure and contents of our dataset without printing everything. By default it returns the first five rows but this can be customized to return any number of rows. It is commonly used to verify that data has been loaded correctly, check column names and inspect the initial records.

Lets see an example of using head() on a DataFrame

We will see how to use the head() method to retrieve the first few rows of the DataFrame(). This provides a quick preview of the dataset’s structure and contents. By default, top 5 rows of dataframe are returned and stored in a new variable. Here we will be using a NBA dataset which you can download from here.

Python
import pandas as pd

data = pd.read_csv("/content/nba.csv")

print("NBA Dataset (First 5 Rows):")
print(data.head())

Output:

head1
Basic example

This is useful for verifying that the data is loaded correctly and for quickly understanding the structure of the dataset.

Syntax:

DataFrame.head(n=5)

Series.head(n=5)

Parameter:

  • n: Number of rows to retrieve from the top of the DataFrame or Series.

Return: It returns the first n rows of the DataFrame or Series as a new DataFrame or Series.

Example of head() method

Lets see other examples for its better understanding.

1. Using head() with a Custom Number of Rows

While the default number of rows returned by head() is 5 but we can customize this number to preview a specific subset of the data. This is useful when we want to see more or fewer rows.

Python
import pandas as pd

data = pd.read_csv("/content/nba.csv")

series = data["Name"]

top = series.head(n = 7)

print("NBA Dataset (First 7 Rows):")
print(top)

Output:

head2
Custom Number of Rows

By specifying 7, we retrieve the first seven rows of the dataset. This is helpful when we need to inspect a larger portion of the data than the default without printing the entire set.

2. Using head() on a Series

The head() method can also be used on a Pandas Series to retrieve the first few elements. This is useful when we're working with a single column of data and want to quickly inspect its contents.

Python
import pandas as pd

data = pd.read_csv("/content/nba.csv")

salary = data['Salary']

print("First 5 Salaries:")
print(salary.head())

Output:

head3
head() on a Series

We retrieve the first 5 salary entries from the "Salary" column.

3. Describing Specific Columns with head()

We can also use the head() method to preview specific columns of our dataset. This example focuses on previewing just the "Name" and "Salary" columns.

Python
import pandas as pd

data = pd.read_csv("/content/nba.csv")

salary_name = data[['Name', 'Salary']].head()

print("First 5 Rows of Name and Salary Columns:")
print(salary_name)

Output:

head4
Specific Columns with head()

We preview the first 5 rows of just the "Name" and "Salary" columns, allowing us to focus on these specific features.

4. Using head() After Sorting a DataFrame

Here we will use head() to inspect the first few rows of a DataFrame after sorting it by a specific column. This is useful when we want to identify the top records based on a specific criterion like the highest salary or the youngest player.

Python
import pandas as pd

data = pd.read_csv("/content/nba.csv")

sorted_data = data.sort_values(by='Age', ascending=True)

top_sorted = sorted_data.head()

print("First 5 Rows After Sorting by Age:")
print(top_sorted)

Output:

head5
head() After Sorting

After sorting the dataset by the "Age" column, we use head() to retrieve the first 5 rows which now correspond to the youngest players in the NBA dataset.

By mastering the head() method, we can easily navigate through our data which ensures a smooth start to our analysis and making it easier to spot key insights right from the beginning.


Next Article

Similar Reads