Python Pandas program to get a list of column headers from the DataFrame

In this python pandas program, we will get a list of column headers from the DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Get a list of column headers from the DataFrame using list(df.columns.values).
  4. Print the output.
				
					import pandas as pd
d = {'name':['Virat','Messi','Kobe'],'sport':['cricket','football','basketball']}
df = pd.DataFrame(d)
print("Dataframe: \n",df)
print("Names of columns: ")
print(list(df.columns.values))
				
			

Output :

				
					Dataframe: 
     name       sport
0  Virat     cricket
1  Messi    football
2   Kobe  basketball
Names of columns: 
['name', 'sport']
				
			

iterate over rows in a DataFrame

rename columns of a given DataFrame

Leave a Comment