在按另一列的键分组后,如何添加两列
例如,我有下表:
+------+------+------+
| 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' ]])
有没有更短的方法来解决这个问题?
答案 0 :(得分:2)
mydf.groupby('Col1').sum().sum(axis=1)
答案 1 :(得分:0)
使用以下内容:
mydf['Sum of Val1 and Val2'] = mydf['Val1'] + mydf['Val2']
df = mydf.groupby('Col1')['Sum of Val1 and Val2'].sum().reset_index()
print(df)
Col1 Sum of Val1 and Val2
0 1 15
1 2 5
2 3 21
3 4 3
4 5 2