我正在尝试组织一个电子表格来跟踪项目流程。
目标是对具有相同操作、价格和日期的项目的金额列求和。
举个例子:
Item action amount price date
socks buy 10 $20 5/1
socks buy 5 $20 5/1
socks sell 5 $20 5/1
shoes sell 7 $25 5/2
shoes sell 2 $25 5/2
shoes sell 8 $30 5/2--会变成--
socks buy 15 $20 5/1
socks sell 5 $20 5/1
shoes sell 9 $25 5/2
shoes sell 8 $30 5/2有没有可能使用熊猫呢?
发布于 2020-06-04 02:28:34
使用DataFrame.groupby对'Item', 'action', 'price', 'date'上的数据帧进行分组,然后使用agg函数sum计算每个组的amount列的总和,然后使用DataFrame.reset_index重置分组数据帧的索引:
df = df.groupby(['Item', 'action', 'price', 'date']).sum().reset_index().reindex(columns=df.columns)结果:
# print(df)
Item action amount price date
0 shoes sell 9 $25 5/2
1 shoes sell 8 $30 5/2
2 socks buy 15 $20 5/1
3 socks sell 5 $20 5/1https://stackoverflow.com/questions/62180004
复制相似问题