[英]Pandas.DataFrame.groupby.sum() drops columns
I have the following dataset:我有以下数据集:
Assignment![]() |
Reference![]() |
Amount![]() |
Order reason![]() |
---|---|---|---|
BB017648 ![]() |
90317000 ![]() |
1,579.54 ![]() |
PEN![]() |
BB017648 ![]() |
90748514 ![]() |
3,999.00 ![]() |
|
BB017648 ![]() |
90317000 ![]() |
540.21 ![]() |
|
BB001947 ![]() |
90464822 ![]() |
33,003.89 ![]() |
PEN![]() |
BB017244 ![]() |
90687323 ![]() |
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.