How to Get the List of Column Names or Headers in Pandas

Spread the love

Problem –

You have a pandas dataframe and you want to get the list of column names or headers in pandas.

Solution –

Let’s read a dataset to illustrate.

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv')
df.head()

In Pandas, the dataframe.columns attribute gives you the names of the columns in a pandas dataframe. And to convert it into a list you can use tolist() method.

df.columns.tolist()

You can also write

list(df.columns.values)

Or simply

list(df)

All of the methods works.

Rating: 1 out of 5.

Leave a Reply