我有一个如下所示的数据框:
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
我想创建四个列中的列表,所以我执行以下操作:
ttp = list(dataframe.ttp)
cap_util = list(dataframe.cap_util)
wip = list(dataframe.wip)
var = list(dataframe.var)
它适用于所有这些,除了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
为什么会发生此错误以及如何解决?
答案 0 :(得分:2)
您可以使用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]
发生错误是因为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]
最好使用方括号[]
来访问您的列,而不是作为属性来避免此类并发症
var
是你的df上的一个方法,它会返回方差:
In [132]:
df.var()
Out[132]:
ttp 14.897950
var 3313.733384
cap_util 0.071927
wip 278.553319
dtype: float64
由于此类方法不可迭代,因此基本上您的列名与方法名相同,因此通过尝试作为属性访问方法名称优先,以避免歧义使用方括号df['var']