Pandas sum()TypeError:只有length1数组可以转换为Python标量

2024-10-03 11:14:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类似于pandas的数据帧,叫做gps

   a  b  label  importe
1  x  y    1     500.2
2  y  z    0     300.4
3  z  x    0     200.3
4  x  z    2     300.6
5  y  y    1     200.7

我想在一个数组中添加具有相同标签的“import”的和,因此:

^{pr2}$

给了我

[500.7, 700.9, 300.6]

我想要,但是这样:

print [gps.importe[gps.label == i].sum() for i in range(3)]

给了我

TypeError: only length-1 arrays can be converted to Python scalars

实际上,我在pandas数据帧上有更多的值和3个以上的标签,这就是为什么我需要第二个选项来工作。在

为什么会出现这个错误?我做错什么了?有什么办法?在


Tags: 数据inimportpandasforrange标签数组