In this python pandas program, we will print the first n rows of a Dataframe using pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a dictionary.
- Now convert that dictionary to DataFrame using pd.DataFrame().
- 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