Pandas Head – How to get the first few rows?

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.
# 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.

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

# Apply head function
seriesA.head()
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

# 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]
99

Applying Head function

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.

# 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.

# 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.

# 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

# 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:

Want to become awesome in ML?

Hi! I am Selva, and I am excited you are reading this!
You can now go from a complete beginner to a Data Science expert, with my end-to-end free Data Science training.
No shifting between multiple books and courses. Hop on to the most effective way to becoming the expert. (Includes downloadable notebooks, portfolio projects and exercises)


Start free with the first course 'Foundations of Machine Learning' - a well rounded orientation of what the field of ML is all about.

Enroll to the Foundations of ML Course (FREE)

Sold already? Start with the Complete ML Mastery Path

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

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