简体   繁体   English

Pandas.DataFrame.groupby.sum() 删除列

[英]Pandas.DataFrame.groupby.sum() drops columns

I have the following dataset:我有以下数据集:

Assignment任务 Reference参考 Amount数量 Order reason订购原因
BB017648 BB017648 90317000 90317000 1,579.54 1,579.54 PEN
BB017648 BB017648 90748514 90748514 3,999.00 3,999.00
BB017648 BB017648 90317000 90317000 540.21 540.21
BB001947 BB001947 90464822 90464822 33,003.89 33,003.89 PEN
BB017244 BB017244 90687323 90687323 10.16 10.16 REJ瑞杰

I would like to perform a "pivot table like" aggregation on the column "Reference".我想在“参考”列上执行“数据透视表”聚合。 I tried this using the following code:我使用以下代码进行了尝试:

import pandas as pd

wb = pd.read_excel("file.XLSX")
wb = wb.groupby("Reference").sum()

However, the result drops the columns "Order Reason" and "Assignment" and only shows the Reference and Amount.但是,结果会删除“订单原因”和“分配”列,仅显示参考和金额。

Is there a way to prevent this?有没有办法防止这种情况?

You can groupby more than one column.您可以对多个列进行分组。 Something like this should get what you want?像这样的东西应该得到你想要的吗?

wb.groupby(["Reference", "Order reason", "Assignment"]).sum()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM