【发布时间】:2018-05-29 00:05:23
【问题描述】:
假设我有一张如下所示的表格:
Company Region Date Count Amount
AAA XXY 3-4-2018 766 8000
AAA XXY 3-14-2018 766 8600
AAA XXY 3-24-2018 766 2030
BBB XYY 2-4-2018 66 3400
BBB XYY 3-18-2018 66 8370
BBB XYY 4-6-2018 66 1380
我想去掉 Date 列,然后按 Company AND region 聚合以求 Count 和 sum of Amount 的平均值 .
预期输出:
Company Region Count Amount
AAA XXY 766 18630
BBB XYY 66 13150
我在这里查看了这篇文章以及许多其他在线文章,但似乎它们只执行一种聚合操作(例如,我可以按多列聚合,但只能产生一列输出作为总和或计数, NOT sum AND count)
有人可以帮忙吗?
我做了什么:
我在这里关注了这篇文章:
https://www.shanelynn.ie/summarising-aggregation-and-grouping-data-in-python-pandas/
但是,当我尝试使用本文中介绍的方法时(接近文章末尾),通过使用字典:
aggregation = {
'Count': {
'Total Count': 'mean'
},
'Amount': {
'Total Amount': 'sum'
}
}
我会收到这个警告:
FutureWarning: using a dict with renaming is deprecated and will be removed in a future version
return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
我知道它现在可以工作,但我想确保我的脚本以后也可以工作。以后如何更新我的代码以兼容?
【问题讨论】:
-
请同时发布您的预期输出。
-
@HaleemurAli 添加了!
标签: python pandas pandas-groupby