pandas 获取 DataFrame 列作为值列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35747137/
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
Get DataFrame column as list of values
提问by Charon
I'm trying to get the columns of a pandas DataFrame
as a list of values.
我试图将Pandas的列DataFrame
作为值列表。
I can access the first column using iloc
:
我可以使用以下方法访问第一列iloc
:
df.ix[:,[0]].values
However, that returns an array of lists:
但是,它返回一个列表数组:
>>> df3.ix[:,[1]].values
array([[ 0.],
[ 0.],
[ 0.],
How can I return a list of numbers?
如何返回数字列表?
I can get what I want by calling the column by name and using tolist()
:
我可以通过按名称调用列并使用tolist()
:
>>> df3['D-328'].tolist()
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 15.0,
However, when calling the column by index, that method is not available:
但是,当按索引调用列时,该方法不可用:
>>> df3.ix[:,[0]].tolist()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 2360, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'tolist'