我有一个数据帧如下:熊猫GROUPBY和
ref, type, amount
001, foo, 10
001, foo, 5
001, bar, 50
001, bar, 5
001, test, 100
001, test, 90
002, foo, 20
002, foo, 35
002, bar, 75
002, bar, 80
002, test, 150
002, test, 110
这就是我试图让:
ref, type, amount, foo, bar, test
001, foo, 10, 15, 55, 190
001, foo, 5, 15, 55, 190
001, bar, 50, 15, 55, 190
001, bar, 5, 15, 55, 190
001, test, 100, 15, 55, 190
001, test, 90, 15, 55, 190
002, foo, 20, 55, 155, 260
002, foo, 35, 55, 155, 260
002, bar, 75, 55, 155, 260
002, bar, 80, 55, 155, 260
002, test, 150, 55, 155, 260
002, test, 110, 55, 155, 260
所以我有这样的:
df.groupby('ref')['amount'].transform(sum)
但我如何过滤它以使上述仅适用于行type=foo
或bar
或test
?
@EdChum是的,我可以过滤数据帧,但我需要三个新的列'ref'和类型'总和'。如果这是有道理的? – Kvothe
那么,为什么不groupby在裁判和类型呢? – EdChum
我可以在参考和类型groupby,但列将如何工作?因为我想为每个类型的值添加总和。 – Kvothe