Python 要列出的熊猫列名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27129152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:28:10  来源:igfitidea点击:

pandas column names to list

pythonpandas

提问by Moritz

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 ' . But i would like to just get the clean names as list without any hack around. Is that possible ?

我知道,我可以摆脱 u 和 ' 。但我只想获得干净的名称作为列表,而无需任何黑客攻击。那可能吗 ?

采纳答案by gincard

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']

回答by Simeon Visser

The list [u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']contains Unicode strings: the uindicates 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 HOWTOfor more details on Unicode strings in Python 2.x.

该列表[u'q_igg', u'q_hcp', u'c_igg', u'c_hcp']包含 Unicode 字符串:u表示它们是 Unicode 字符串,'并且包含在每个字符串周围。您现在可以在代码中以任何方式使用这些名称。有关Python 2.x 中 Unicode 字符串的更多详细信息,请参阅Unicode HOWTO

回答by chrisb

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]

回答by PlagTag

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.

如前所述, u 表示它的 unicode 已转换。无论如何,最干净的方法是将 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. I would not use that but it might befit your needs in this special case ;-)

一个更脏的解决方案是获取列表对象的字符串表示并替换 u。我不会使用它,但在这种特殊情况下它可能适合您的需求;-)

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

回答by brijesh_patel

Simple and easy way: df-dataframe variable name

简单易行的方法:df-dataframe 变量名

df.columns.to_list()

this will give the list of the all columns name.

df.columns.to_list()

这将给出所有列名称的列表。