Python pandas program to print the selected columns from DataFrame

In this python pandas program, we will print the selected columns 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 columns from DataFrame using df[[‘Name’,’Age’]].
  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(df[['Name','Age']])
				
			

Output :

				
					Original dataframe: 
    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
    Name  Age
0   Alex   30
1   John   27
2  Peter   29
				
			

print the last n rows of a DataFrame

print the selected rows from DataFrame

Leave a Comment