Python Pandas:instancemethod 对象不可迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32136067/
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-13 23:48:08  来源:igfitidea点击:

Python Pandas: instancemethod object is not iterable

pythonpandas

提问by manuelschipper

I have a dataframe that looks like this:

我有一个看起来像这样的数据框:

      id        ttp           var  cap_util        wip
0   ADLL   3.333333  6.003725e-28  0.327531   3.258475
1   ADLM   2.000000  0.000000e+00  0.197834   1.970107
2   ADLH   1.428571  1.012494e-28  0.140841   1.407658
3   ADML   2.631579  1.012897e-28  0.868789   8.640131
4   ADMM   1.724138  7.889188e-28  0.567057   5.689792
5   ADMH   1.369863  5.961075e-28  0.453626   4.528798
6   ADHL   1.923077  1.014326e-28  0.951970   9.513957
7   ADHM   1.538462  1.437187e-29  0.762675   7.625510
8   ADHH   1.282051  1.974665e-29  0.635893   6.372434
9   ASLL   5.041522  2.684441e+01  0.345550   5.104609
10  ASLM   2.351336  5.567464e+00  0.175023   2.034605
11  ASLH   1.775086  3.101865e+00  0.153913   1.872913
12  ASML  12.662366  1.565324e+02  0.818762  38.526489
13  ASMM   3.754160  1.401226e+01  0.577600  12.223828
14  ASMH   2.358363  5.185841e+00  0.438072   7.880536
15  ASHL  14.769186  2.008306e+02  0.865977  65.487920
16  ASHM   6.829864  5.845502e+01  0.733780  32.611342
17  ASHH   3.725520  1.640096e+01  0.600177  18.561905

And I want to create lists out of four of the columns so I do the following:

我想从四列中创建列表,所以我执行以下操作:

ttp = list(dataframe.ttp)
cap_util = list(dataframe.cap_util)
wip = list(dataframe.wip)
var = list(dataframe.var)

It works fine for all of them, except var for which I get the following error:

它适用于所有人,除了 var ,我收到以下错误:

Traceback (most recent call last):
  File "07_corrplot_var_physics.py", line 34, in <module>
    all_var = list(master_df.var)
TypeError: 'instancemethod' object is not iterable

Why does this error happen and how can I fix it?

为什么会发生此错误,我该如何解决?

采纳答案by EdChum

You can just use tolist():

你可以只使用tolist()

In [128]:
df['var'].tolist()

Out[128]:
[6.0037249999999998e-28,
 0.0,
 1.012494e-28,
 1.0128969999999999e-28,
 7.8891880000000005e-28,
 5.9610749999999994e-28,
 1.0143259999999999e-28,
 1.437187e-29,
 1.9746649999999998e-29,
 26.844409999999996,
 5.5674640000000002,
 3.1018650000000001,
 156.5324,
 14.012260000000001,
 5.1858409999999999,
 200.8306,
 58.455019999999998,
 16.400960000000001]

The error occurs because varattribute is shadowing your column, for instance this works:

发生错误是因为var属性隐藏了您的列,例如这有效:

In [130]:
list(df['var'])

Out[130]:
[6.0037249999999998e-28,
 0.0,
 1.012494e-28,
 1.0128969999999999e-28,
 7.8891880000000005e-28,
 5.9610749999999994e-28,
 1.0143259999999999e-28,
 1.437187e-29,
 1.9746649999999998e-29,
 26.844409999999996,
 5.5674640000000002,
 3.1018650000000001,
 156.5324,
 14.012260000000001,
 5.1858409999999999,
 200.8306,
 58.455019999999998,
 16.400960000000001]

It is better to use square brackets []to access your columns rather than as an attribute to avoid complications such as this

最好使用方括号[]来访问您的列,而不是作为属性来避免诸如此类的并发症

varis a method on your df, it returns the variance:

var是 df 上的一个方法,它返回方差:

In [132]:
df.var()

Out[132]:
ttp           14.897950
var         3313.733384
cap_util       0.071927
wip          278.553319
dtype: float64

As such methods are not iterable so essentially your column name is the same as a method name so by trying to access as an attribute the method name takes precedence so to avoid the ambiguity use square brackets df['var']

由于此类方法不可迭代,因此本质上您的列名与方法名相同,因此通过尝试作为属性访问,方法名优先,以避免歧义使用方括号 df['var']