python - 为什么 groupby 和 sum 中未提及的列将被删除?
问题描述
我有这个数据框:
InvoiceID PaymentDate TotalRevenue Discount Discount_Revenue
0 72A04E22 2020-07-03 17:25:13 1650000.0 0.0 1650000.0
1 54FCFCB9 2021-03-17 14:26:08 5500000.0 0.0 5500000.0
...
在以下聚合之后,列PaymentDate
被删除:
df.groupby(by=['InvoiceID'])[['TotalRevenue','Discount','Discount_Revenue']].sum().reset_index(drop=True, inplace=True)
如何仍然保留未在 group by 或聚合函数中提及的列?
解决方案
当您groupby
使用sum
它时,意味着您正在聚合数据:从多行相同的行中,InvoiceID
您只保留一个,其中所有行的值的总和为df
.
假设这是您的数据框,同一行两次:
InvoiceID PaymentDate TotalRevenue Discount Discount_Revenue
0 72A04E22 2020-07-03 17:25:13 1650000.0 0.0 1650000.0
1 54FCFCB9 2021-03-17 14:26:08 5500000.0 0.0 5500000.0
2 54FCFCB9 2021-03-17 14:26:08 5500000.0 1.0 5500000.0
然后你可以看到求和Discount
的效果,例如:
>>> df.groupby('InvoiceID')['Discount'].sum()
InvoiceID
54FCFCB9 1.0
72A04E22 0.0
Name: Discount, dtype: float64
具体回答您的问题:该列PaymentDate
被删除,因为您没有指定如何聚合它
对于添加没有意义的列,例如
PaymentDate
,您需要定义另一个要使用的聚合函数。您要保留第一个付款日期吗?最后一个?请注意,
InvoiceID
在上面的示例中并没有消失,您有意在代码中将其删除.reset_index(drop=True)
假设我们选择保留最后付款日期,然后使用reset_index
withoutdrop=True
也保留 InvoiceID,我们有:
>>> invoice_groups = df.groupby('InvoiceID')
>>> invoices = invoice_groups.sum().join(invoice_groups['PaymentDate'].max()).reset_index()
>>> invoices
InvoiceID TotalRevenue Discount Discount_Revenue PaymentDate
0 54FCFCB9 11000000.0 1.0 11000000.0 2021-03-17 14:26:08
1 72A04E22 1650000.0 0.0 1650000.0 2020-07-03 17:25:13
这就是您的所有列,所有列都以某种方式(总和或最大值)从原始数据框中的行聚合。
推荐阅读
- endeca - Oracle Endeca 两种方式词库不显示相同数量的记录
- facebook - 如何使用 Facebook 页面订阅消息
- javascript - setState 在此代码中的工作方式的混乱
- python - 如何在不使用内置函数的情况下从python中的数组中删除元素
- javascript - 启用/禁用多个输入字段JS的更有效方法
- python - 为什么小数不能与浮点数互操作
- c# - 如何在 .net Core 中一次在多个浏览器中运行相同的 specflow 测试?
- visual-studio - Visual Studio 测试资源管理器键盘将焦点设置为测试列表
- powershell - Invoke-Webrequest 单击一个按钮并继续使用会话变量
- python - 平滑数据以确定 Python 中的峰值