In this python pandas program, we will print the selected rows from DataFrame using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Print the selected rows from DataFrame using df.iloc[], it is the same as indexing.
- Print the output.
import pandas as pd
d = {'Sr.no.':[1,2,3],'Name':['Alex','John','Peter'],'Age':[30,27,29]}
df = pd.DataFrame(d)
print("Original dataframe: \n",df)
print("Second row: \n",df.iloc[1,:])
Output :
Original dataframe:
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
Second row:
Sr.no. 2
Name John
Age 27
Name: 1, dtype: object