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.
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 headfunction is applied to a series object, the result is also returned in the form of series.
2. Head function on DataFrame
On applying the headfunction to a dataframe, the result is also returned as a dataframe with fewer rows.
Length of the dataframe
Applying Head function

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.

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.

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.

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

Practical Tips
- 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.
- 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