How to get list from pandas DataFrame column headers

0 votes

I want 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']
Dec 23, 2020 in Python by Roshni
• 10,480 points
978 views

1 answer to this question.

0 votes

You can get the values as a list by doing:

list(my_dataframe.columns.values)

list(my_dataframe)
answered Dec 23, 2020 by Gitika
• 65,770 points

Related Questions In Python

0 votes
1 answer

How to create Pandas dataframe from Python list?

You can do it like this: import ...READ MORE

answered Apr 6, 2019 in Python by Likhita
1,287 views
0 votes
1 answer

How to delete column from pandas DataFrame?

Hi@akhtar, You can use the del command in ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,460 points
1,147 views
0 votes
1 answer

How to create empty pandas dataframe only with column names?

You can do it like this: df=pd.DataFrame(columns=["Name","Old","Ne ...READ MORE

answered Apr 6, 2019 in Python by Hari
10,850 views
0 votes
1 answer

How to get value in Pandas dataframe using index?

You can use the iloc method to do ...READ MORE

answered Apr 8, 2019 in Python by Rohit
8,299 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
3 answers

How to get the return value from a thread using python?

FWIW, the multiprocessing module has a nice interface for ...READ MORE

answered Dec 15, 2020 in Python by Roshni
• 10,480 points
107,746 views
+1 vote
3 answers

How to print one pandas column without index?

There is a slight mistake in what ...READ MORE

answered Mar 28, 2019 in Python by Shri
64,257 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP