pandas column names to list

Question:

According to this thread:
SO: Column names to list

It should be straightforward to do convert the column names to a list. But if i do:

df.columns.tolist()

I do get:

[u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']

I know, i could get rid of the u and the ‘ . But i would like to just get the clean names as list without any hack around. Is that possible ?

Asked By: Moritz

||

Answers:

The list [u'q_igg', u'q_hcp', u'c_igg', u'c_hcp'] contains Unicode strings: the u indicates that they’re Unicode strings and the ' are enclosed around each string. You can now use these names in any way you’d like in your code. See Unicode HOWTO for more details on Unicode strings in Python 2.x.

Answered By: Simeon Visser

If you’re just interested in printing the name without an quotes or unicode indicators, you could do something like this:

In [19]: print "[" + ", ".join(df) + "]"
[q_igg, q_hcp, c_igg, c_hcp]
Answered By: chrisb

As already mentioned the u means that its unicode converted. Anyway, the cleanest way would be to convert the colnames to ascii or something like that.

In [4]: cols
Out[4]: [u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']

In [5]: [i.encode('ascii', 'ignore') for i in cols]
Out[5]: ['q_igg', 'q_hcp', 'c_igg', 'c_hcp'

The problem here is that you would lose special characters that are not encode in ascii.

A much more dirty solution would be to fetch the string representation of the list object and just replace the u. I would not use that but it might befit your needs in this special case 😉

In [7]: repr(cols)
Out[7]: "[u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']"
In [11]: x.replace("u", "")
Out[11]: "['q_igg', 'q_hcp', 'c_igg', 'c_hcp']"

see: https://docs.python.org/2/library/repr.html

Answered By: PlagTag

Or, you could try:

df2 = df.columns.get_values()

which will give you:

array(['q_igg', 'q_hcp', 'c_igg', 'c_hcp'], dtype=object)

then:

df2.columns.tolist()

which gives you:

['q_igg', 'q_hcp', 'c_igg']
Answered By: gincard

Simple and easy way:
df-dataframe variable name

df.columns.to_list()

this will give the list of the all columns name.

Answered By: brijesh_patel

this will do the job

list(df2)
Answered By: Omkar Darves
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.