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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:48:21  来源:igfitidea点击:

Get DataFrame column as list of values

pythonpandasdataframe

提问by Charon

I'm trying to get the columns of a pandas DataFrameas 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'

回答by jezrael

I think you can try ixthis way:

我想你可以试试ix这种方式:

df.ix[:, 0].tolist()

And as mentioned DSMin comments, you can use ilocthis way, if you need select first column by position:

正如评论中提到的DSMiloc,如果您需要按位置选择第一列,您可以使用这种方式:

df.iloc[:, 0].tolist()