Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Get list from pandas DataFrame column headers

I am trying to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I won't know how many columns there will be or what they will be called.

For example, if I'm given a DataFrame like this:

>>> my_dataframe
y gdp cap
0 1 2 5
1 2 3 9
2 8 7 2
3 3 4 7
4 6 7 7
5 4 8 3
6 8 2 8
7 9 9 10
8 6 6 4
9 10 10 7

I would get a list like this:

>>> header_list
['y', 'gdp', 'cap']
by

2 Answers

kshitijrana14
You can get the values as a list by doing:
list(my_dataframe.columns.values)

Also you can simply use:
list(my_dataframe)
sandhya6gczb
You can get the header as a list by simply using this one-liner code.

list(df.columns)

Login / Signup to Answer the Question.