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,520 points
753 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,910 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
957 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,440 points
923 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
9,939 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
7,628 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,070 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,520 points
105,395 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
62,629 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