Menu

Pandas Head – How to get the first few rows?

Written by MachineLearningPlus | 3 min read

pandas.head() function is used to access the first n rows of a dataframe or series. It returns a smaller version of the caller object with the first few entries.

In this article, you will learn how to use the python head function , customizing the number of entries and two more functions that do the same job differently.

pandas.head

  • Syntax: pandas.head(n=5)
  • Purpose: Return the first n rows. This function returns the first n rows for the object based on position. It is useful for quickly testing if your object has the right type of data in it.
  • Parameters:
    • n: int (default 5) Number of rows to select.
  • Returns same type as caller
    • The first n rows of the caller object.
python
# Import packages
import pandas as pd

Pandas or Python Head Function

Head function returns the dataframe or series with the first few rows (by default 5). To perform this function, chain .head() function to the dataframe or series.

1. Head function on Series

When the head function is applied to a series object, the result is also returned in the form of series.

python
# Create a Series
seriesA = pd.Series(list(range(1,100)))

# Apply head function
seriesA.head()
python
0    1
1    2
2    3
3    4
4    5
dtype: int64

2. Head function on DataFrame

On applying the head function to a dataframe, the result is also returned as a dataframe with fewer rows.

Length of the dataframe

python
# Create a dataframe
df = pd.DataFrame({
                    'Subject_1_Marks': list(range(1,100)),
                    'Subject_2_Marks': list(range(1,100)),
                    'Subject_3_Marks': list(range(1,100)),
                    }
                 )

# check the length of the dataframe
len(df)   # or df.shape[0]
python
99

Applying Head function

python
df.head()

How to control the number of rows in the output?

By default, the head function returns only the first 5 rows of the dataset. To control this behavior, you can use the n parameter. It takes in the number of rows you want to display.

python
# Applying head function with n=10
df.head(n=10)

What if n is negative?

If a negative value is passed in the number of rows parameter, n, then the function returns all the rows except the last n rows. It is similar to using the df[:-n] assignment.

python
# Head function with n=-10 
df.head(n=-10)

Other Functions

The head function returns the rows from the beginning of the dataset. You can get the rows from the end using the tail function. Also, the sample function returns a random row from the whole dataset. Let’s implement them separately.

Tail function

It works in the same way as the head function but returns the last few rows.. It can also optionally take the number of rows to be displayed.

python
# tail function with n=7
df.tail(n=7)

Sample function

The sample function returns a random row from the whole dataset. By default, it will return one random row but you can specify the number of rows to be returned using the n parameter.

Note: n should be less than or equal to the length of the dataset in case of replace=False (default case) of sample function

python
# sample function with n=2
df.sample(n=3)

Practical Tips

  1. It is a good practice to look at some rows of the dataset irrespective of the position. You can check for first rows, last rows, or any random row.
  2. Head function is useful for quickly testing if the dataset contains the right type of data.

Test your knowledge

Q1: Head function can take negative values. True or False?

Answer:

Answer: True. The function returns all the rows except the last the n rows.

Q2: What is the difference between head and tail function?

Answer:

Answer: Tail function returns rows from the end of the dataset while head function returns rows from the beginning.

 

The article was contributed by Kaustubh G and Shri Varsheni

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master Pandas — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science