简体   繁体   English

要列出的熊猫列名称

[英]pandas column names to list

According to this thread: SO: Column names to list根据此线程: SO:要列出的列名

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 ' .我知道,我可以摆脱 u 和 ' 。 But i would like to just get the clean names as list without any hack around.但我只想获得干净的名称作为列表,而无需任何黑客攻击。 Is that possible ?那可能吗 ?

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.tolist()

which gives you:这给了你:

['q_igg', 'q_hcp', 'c_igg']

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.列表[u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']包含 Unicode 字符串: u表示它们是 Unicode 字符串,而'括在每个字符串周围。 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.有关 Python 2.x 中 Unicode 字符串的更多详细信息,请参阅Unicode HOWTO

Simple and easy way: df-dataframe variable name简单易行的方法:df-dataframe 变量名

df.columns.to_list()

this will give the list of the all columns name.这将给出所有列名称的列表。

If you're just interested in printing the name without an quotes or unicode indicators, you could do something like this:如果您只想打印不带引号或 unicode 指示符的名称,您可以执行以下操作:

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

As already mentioned the u means that its unicode converted.如前所述, u 表示它的 unicode 已转换。 Anyway, the cleanest way would be to convert the colnames to ascii or something like that.无论如何,最干净的方法是将 colnames 转换为 ascii 或类似的东西。

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.这里的问题是您会丢失未以 ascii 编码的特殊字符。

A much more dirty solution would be to fetch the string representation of the list object and just replace the u.一个更脏的解决方案是获取列表对象的字符串表示并替换 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见: https : //docs.python.org/2/library/repr.html

这将完成工作

list(df2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM