Python Pandas program to print the selected rows from DataFrame

In this python pandas program, we will print the selected rows from DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Print the selected rows from DataFrame using df.iloc[], it is the same as indexing.
  4. 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
				
			

print the selected columns from DataFrame

select the rows where the age is greater than 29

Leave a Comment