What is Pandas DataFrame.head() in Python?
Overview
Python has many modules, but pandas is one of the best at analyzing data. The DataFrame.head() function prints the first n number of rows from the top of DataFrame. It is very useful in data analysis to check dataset attributes.
Syntax
# function syntaxDataFrame.head(n)
Parameter
It takes the following value as an argument:
n: This is the of rows or observations to extract from the head ofnumber integer value DataFrame.
Ifn < 0, it will return lastnrows ofDataFrame.
Return value
It returns segmented DataFrame as a series or DataFrame.
Code
#Import panda moduleimport pandas as panda#Import NumPy moduleimport numpy as numpy#Dataframe of students datastudents = panda.DataFrame({'Name': ['John', 'Rick', 'Alex', 'Ryan', 'Will', 'Alice'],'Marks' : ['15', '19', '10', '13', '15', '17']})#Print the first 4 rows from students dataframeprint(students.head(4))
Explanation
- Lines 2 and 4: We import the
pandaandnumpymodules. - Line 6: We add data in two columns of
NameandMarkswith respective data in it. - Line 10: We print the first four rows from the
studentsusing theDataFrame.head()function.