Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas head() method is used to return top n (5 by default) rows of a data frame or series.
Syntax: Dataframe.head(n=5)
Parameters:
n: integer value, number of rows to be returned
Return type: Dataframe with top n rows
To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.

Example #1:
In this example, top 5 rows of data frame are returned and stored in a new variable. No parameter is passed to .head() method since by default it is 5.
import pandas as pd
data_top = data.head()
data_top
|
Output:
As shown in the output image, it can be seen that the index of returned rows is ranging from 0 to 4. Hence, top 5 rows were returned.

Example #2: Calling on Series with n parameter()
In this example, the .head() method is called on series with custom input of n parameter to return top 9 rows of the series.
import pandas as pd
n = 9
series = data["Name"]
top = series.head(n = n)
top
|
Output:
As shown in the output image, top 9 rows ranging from 0 to 8th index position were returned.

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
01 Oct, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...