In this python pandas program, we will print the last n rows of a DataFrame using pandas.
Steps to solve the program
- Import pandas library as pd.
- Create a dictionary.
- Now convert that dictionary to DataFrame using pd.DataFrame().
- Print the last n rows of a Dataframe using df.tail(n).
import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
print("Last n rows: \n",df.tail(2))
Output :
0 marks1 marks2
0 34 36
1 20 22
2 32 10
3 30 44
Last n rows:
marks1 marks2
2 32 10
3 30 44