Python pandas program to print the first n rows of a Dataframe

In this python pandas program, we will print the first n rows of a Dataframe using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dictionary.
  3. Now convert that dictionary to DataFrame using pd.DataFrame().
  4. Print the first n rows of a Dataframe using df.head(n).
				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
print("First n rows: \n",df.head(2))
				
			

Output :

				
					0   marks1  marks2
0      34      36
1      20      22
2      32      10
3      30      44
First n rows: 
    marks1  marks2
0      34      36
1      20      22
				
			

convert a dictionary into DataFrame

print the last n rows of a DataFrame

Leave a Comment