How to print top rows of Dataframe in python?

Pandas DataFrame.head()

The head() returns the first n rows for the object based on position. If your object has the right type of data in it, it is useful for quick testing. This method is used for returning top n (by default value 5) rows of a data frame or series.

Syntax

DataFrame.head(n=5)

Parameters

n: It refers to an integer value that returns the number of rows.

Return

It returns the DataFrame with top n rows.

Example1

info = pd.DataFrame({'language':['C', 'C++', 'Python', 'Java','PHP']})  
info.head()  
info.head(3)  

Output

   language
0        C
1       C++
2       Python

Example 2
We have a csv file “aa.csv” that have the following dataset.

 Name               Hire Date    Salary      Leaves Remaining  
0   John Idle           03/15/14     50000.0       10  
1   Smith Gilliam       06/01/15     65000.0       8  
2   Parker Chapman      05/12/14     45000.0       10  
3   Jones Palin         11/01/13     70000.0       3  
4   Terry Gilliam       08/12/14     48000.0       7  

By using the head() in the below example, we showed only top 2 rows from the dataset.

# importing pandas module   
import pandas as pd    
# making data frame   
data = pd.read_csv("aa.csv")     
# calling head() method    
# storing in new variable   
data_top  = data.head(2)    
# display   
data_top  
   Name          Hire Date      Salary      Leaves Remaining
0     John Idle        03/15/14     50000.0        10
1     Smith Gilliam    06/01/15     65000.0        8