DF1:
Product Party x y Currency Parent Child
Purchase 5AAA64870 2 0.1 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone
Purchase 5AAA64870 1 0.3 USD Apple Smartphone预期产出:
Product Party x y Currency Parent Child
Purchase 5AAA64870 7 1.6 USD Apple Smartphone代码:
df1 = df1[df1['Product'] == 'Purchase'].groupby(
['Product', 'Party'])['x', 'y'].sum()对不起,这听起来很简单,但是我无法像上面期望的输出那样将数据聚合到一行中。感谢你在这方面的帮助
发布于 2022-08-22 17:39:02
这很容易:)。但是仅仅因为你是一只新蜜蜂,我会解释它背后的逻辑,所以当你群并传递一个列表时。熊猫基本上说,好吧,这就是现在的指数,所以如果指数等于另一个指数,我会把它们放在一起。如果您添加索引列,那么他将根据您的元素将它们组合在一起。
df1[df1['Product'] == 'Purchase'].groupby(
['Product', 'Party','Parent','Currency','Child'],as_index=False)['x', 'y'].sum()https://stackoverflow.com/questions/73448915
复制相似问题