Pandas groupby 并添加组的总和
Pandas groupby and add sum of group
A B C D
1 foo 12 California
2 foo 22 California
3 bar 8 Rhode Island
4 bar 32 Rhode Island
Required output
A B C D
1 foo 12 California
2 22 California
foo Total 34
3 bar 8 Rhode Island
4 32 Rhode Island
bar Total 40
想在每组末尾添加数字列的总数
这里有两种方法。
使用 groupy
+apply
:
(df.set_index('A')
.groupby('B', as_index=False, sort=False)
.apply(lambda d: pd.concat([d, d['C'].agg({'Total': 'sum'}).to_frame()]))
.droplevel(0).rename_axis('A')
.reset_index()
.fillna('')
)
输出:
A B C D
0 1 foo 12 California
1 2 foo 22 California
2 Total 34
3 3 bar 8 Rhode Island
4 4 bar 32 Rhode Island
5 Total 40
使用 concat
和 groupby
+agg
:
(pd.concat([df,
df.groupby('B', as_index=False).agg({'A': lambda x: 'Total', 'C': 'sum'})
])
.sort_values(by=['B', 'A'])
.assign(B=lambda d: d['B'].mask(d['A'].eq('Total')))
.fillna('')
)
输出:
A B C D
2 3 bar 8 Rhode Island
3 4 bar 32 Rhode Island
0 Total 40
0 1 foo 12 California
1 2 foo 22 California
1 Total 34
A B C D
1 foo 12 California
2 foo 22 California
3 bar 8 Rhode Island
4 bar 32 Rhode Island
Required output
A B C D
1 foo 12 California
2 22 California
foo Total 34
3 bar 8 Rhode Island
4 32 Rhode Island
bar Total 40
想在每组末尾添加数字列的总数
这里有两种方法。
使用 groupy
+apply
:
(df.set_index('A')
.groupby('B', as_index=False, sort=False)
.apply(lambda d: pd.concat([d, d['C'].agg({'Total': 'sum'}).to_frame()]))
.droplevel(0).rename_axis('A')
.reset_index()
.fillna('')
)
输出:
A B C D
0 1 foo 12 California
1 2 foo 22 California
2 Total 34
3 3 bar 8 Rhode Island
4 4 bar 32 Rhode Island
5 Total 40
使用 concat
和 groupby
+agg
:
(pd.concat([df,
df.groupby('B', as_index=False).agg({'A': lambda x: 'Total', 'C': 'sum'})
])
.sort_values(by=['B', 'A'])
.assign(B=lambda d: d['B'].mask(d['A'].eq('Total')))
.fillna('')
)
输出:
A B C D
2 3 bar 8 Rhode Island
3 4 bar 32 Rhode Island
0 Total 40
0 1 foo 12 California
1 2 foo 22 California
1 Total 34