简体   繁体   English

Python Pandas:instancemethod对象不可迭代

[英]Python Pandas: instancemethod object is not iterable

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? 为什么会发生此错误,我该如何解决?

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 var attribute 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 最好使用方括号[]来访问列,而不是作为属性来避免此类并发症

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

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

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