首页 > 解决方案 > 用 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' ]])

有没有更短的方法来处理这个?

标签: pandasgroup-by

解决方案


mydf.groupby('Col1').sum().sum(axis=1)

推荐阅读