Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas: instancemethod object is not iterable

Tags:

python

pandas

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:

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?

like image 548
manuelschipper Avatar asked Mar 23 '26 09:03

manuelschipper


1 Answers

You can just use 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:

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:

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

like image 63
EdChum Avatar answered Mar 25 '26 23:03

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!