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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Get a list of column headers from the DataFrame using list(df.columns.values).
- 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']