Python Practice › Pandas Programs › Program 48
Column headers
Write a pandas program to get a list of column headers.
Output
['name', 'sport']
Approach
df.columns.tolist().
Solution
import pandas as pddf = pd.DataFrame({"name": ["Virat", "Messi", "Kobe"], "sport": ["cricket", "football", "basketball"]})print(df.columns.tolist())Output
['name', 'sport']
Learn the concept
New to this topic? Read the lesson, then come back.