The Wayback Machine - https://web.archive.org/web/20250116091018/https://www.geeksforgeeks.org/python-pandas-dataframe-series-head-method/
Open In App

Python | Pandas Dataframe/Series.head() method

Last Updated : 01 Oct, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.




# importing pandas module
import pandas as pd
  
# making data frame
  
# calling head() method 
# storing in new variable
data_top = data.head()
  
# display
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.




# importing pandas module
import pandas as pd
  
# making data frame
  
# number of rows to return
n = 9
  
# creating series
series = data["Name"]
  
# returning top n rows
top = series.head(n = n)
  
# display
top


Output:
As shown in the output image, top 9 rows ranging from 0 to 8th index position were returned.


Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article

Similar Reads

three90RightbarBannerImg