pandas - 用 groupby 添加两列
问题描述
如何在按另一列的键分组后添加两列,
例如我有下表:
+------+------+------+
| Col1 | Val1 | Val2 |
+------+------+------+
| 1 | 3 | 3 |
| 1 | 4 | 2 |
| 1 | 2 | 1 |
| 2 | 2 | 0 |
| 2 | 3 | 0 |
| 3 | 2 | 9 |
| 3 | 2 | 8 |
| 4 | 2 | 1 |
| 5 | 1 | 1 |
+------+------+------+
我想要实现的是
+------+----------------------+
| Col1 | Sum of Val1 and Val2 |
+------+----------------------+
| 1 | 15 |
| 2 | 5 |
| 3 | 21 |
| 4 | 3 |
| 5 | 2 |
+------+----------------------+
我可以得到一个列分组 Col1、Col1 的总和,然后添加他们的结果,但我在这个过程中创建了多个列。
import pandas as pd
data =[[1,3,3],[1,4,2],[1,2,1],[2,2,0],[2,3,0],[3,2,9],[3,2,8],
[4,2,1],[5,1,1]]
mydf = pd.DataFrame(data, columns = ['Col1','Val1','Val2'])
print(mydf)
mydf['total1'] = mydf.groupby('Col1')['Val1'].transform('sum')
mydf['total2'] = mydf.groupby('Col1')['Val2'].transform('sum')
mydf['Sum of Val1 and Val2'] = mydf['total1'] + mydf['total2']
mydf = mydf.drop_duplicates('Col1')
print(mydf[['Col1', 'Sum of Val1 and Val2' ]])
有没有更短的方法来处理这个?
解决方案
mydf.groupby('Col1').sum().sum(axis=1)
推荐阅读
- python-3.x - Python - 无法在 FTP 上上传文件
- php - 更改特定产品类别的 WooCommerce 产品数量设置
- database - Delphi - 复制在 DBGrid 上选择的记录并将其插入到另一个 ADO 表中
- forms - Symfony 4 Form FileType 约束问题
- python - 使用列表合并两个数据框到使用 pandas/python 的单值映射
- flutter - 以编程方式在颤振中创建 docx 和 pdf 文件
- mongodb - Mongo DB 中的类别计数
- nattable - 排序后表格数据消失
- sql - 带有有效期的产品条码
- python - Celery 群发任务