Updated March 31, 2023
Introduction to Pandas DataFrame.head()
Pandas Dataframe.head() function is used to print the first ‘n’ rows of the Dataframe. The index is considered as the first 5 rows in the given Dataframe.
Syntax:
Dataframe.head(n=5)
Where n is the number of rows to be selected from the Dataframe and printed. It is always an integer. It returns the top n rows back to the Dataframe.
- Pandas empower you to make two new sorts of Python questions: The Pandas Series and the Pandas DataFrame.
- These two structures are connected. In this instructional exercise, we are going to concentrate on the DataFrame, yet we should rapidly discuss the Series, so you get it.
- Pandas Dataframe.head() function works in the Pandas series as well. Here, we just learn about how to implement it in Pandas Dataframe.
How Pandas Dataframe.head() function works?
Now, we implement a few examples and show how this .head() function works in Pandas.
Example #1
Code:
import pandas as pd
data = {
'country':['Canada', 'Mexico', 'India', 'Switzerland', 'Belgium', 'Japan', 'South Africa']
,'continent': ['America', 'America', 'Asia', 'Europe', 'Europe', 'Asia', 'Africa']
}
df = pd.DataFrame(data, columns = ['country','continent'])
df.head()
print(df.head())
Output:
Explanation: In the above program, we see that we have imported pandas as pd and later created the indices for 2 columns ‘country’ and the respective ‘continent’. We then invoke this Dataframe into a variable named df and finally use the pandas dataframe.head() function to print the first ‘n’ rows of the Dataframe. So the system by default considers the first 5 rows and prints the 2 columns country and continent respectively and returns the output back to the Dataframe.
Example #2
Code:
import pandas as pd
data = {
'country':['Canada', 'Mexico', 'India', 'Switzerland', 'Belgium', 'Japan', 'South Africa']
,'continent': ['America', 'America', 'Asia', 'Europe', 'Europe', 'Asia', 'Africa']
}
df = pd.DataFrame(data, columns = ['country','continent'])
df.head(4)
print(df.head(4))
Output:
Explanation: From the above code, we see that a specific integer value is given inside the head() function. This prints out the first 4 rows of the given Dataframe and finally returns back to the caller function. Hence, we have given the function of df.head(4).
Example #3
Code:
import pandas as pd
data = {
'animals':['lizard', 'Snake', 'Elephant', 'Crocodile', 'Whale']
,'species': ['Reptile', 'Reptile', 'Mammal', 'Reptile', 'Mammal']
}
df = pd.DataFrame(data, columns = ['animals','species'])
df.head(3)
print(df.head(3))
Output:
Explanation: In the above program, we consider a different set of data which is a set of animals and the respective species to which these belong in two different columns. Now, we want to print only the first 3 rows using the Pandas Dataframe.head() function. Hence, we invoke the head() function as df.head(3) and thus it prints the output of the first 3 rows of animals and the species.
Example #4
Code:
import pandas as pd
data = {
'animals':['lizard', 'Snake', 'Elephant', 'Crocodile', 'Whale']
,'species': ['Reptile', 'Reptile', 'Mammal', 'Reptile', 'Mammal']
}
df = pd.DataFrame(data, columns = ['animals','species'])
df.head()
print(df.head())
Output:
Explanation: Here, we first import pandas as pd and then describe all the indices in the dataframe animal and species. Then finally we invoke the pandas dataframe.head() function and assign the data and columns in the dataframe. Hence, if we do not provide the required integer in the brackets and leave it blank, the console prints all the rows and columns by default.
Difference between Pandas Dataframe.head() and .tail() function
The main difference between Pandas Dataframe.head() and Pandas Dataframe.tail() functions are that the .head() function is used to print the first n rows of the Dataframe while the .tail() function is used to print the last n rows of the Dataframe. These functions work similarly with respect to the code but the representation of the indices with these functions will be different while printing the output.
Conclusion
I would like to conclude by saying that Pandas Dataframe.head() function helps to select the first n rows in the Dataframe. Pandas is an information control bundle for the Python programming language. In spite of the fact that Pandas and NumPy both give information control apparatuses; they center around various things. NumPy basically centers around numeric information that is organized in a cluster. Truth be told, NumPy solely works with numeric information. Numeric information in Python. Critically, NumPy clusters can be 1-dimensional, 2-dimensional, or multi-dimensional. So, in spite of the fact that they are constrained in that they should contain numeric information, they are increasingly adaptable in that they can have a subjective number of measurements. This can make them magnificent for specific kinds of AI undertakings (like profound learning). Pandas, then again, has an alternate core interest. Pandas generally center around an information structure called the “DataFrame,” which are carefully 2-dimensional (not at all like the NumPy cluster) and contain heterogeneous sections (additionally not at all like the NumPy exhibit). Since we are discussing the DataFrame, how about we examine the two information structures of Pandas – the Series and the DataFrame – and how they are connected.
Recommended Articles
We hope that this EDUCBA information on “Pandas DataFrame.head()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.