Summarize Grouped Data In Pandas While Filtering After The Group Part
I am stumbling over the exact (or at least most elegant) steps to group and aggregate some data in Pandas. Let's say I have a DataFrame that looks something like this - system
Solution 1:
You can go by using the power of apply function:
def conditional_sum(grp):
grp['non_bar_total'] = grp[grp.datatype != 'bar']['datacount'].sum()
return grp
df.groupby(['system', 'sub_system']).apply(conditional_sum)
system sub_system datatype datacount non_bar_total
0 a 1 foo 111 444
1 a 1 bar 222 444
2 a 1 baz 333 444
3 a 2 foo 444 999
4 a 2 baz 555 999
5 b 1 foo 667 667
6 b 3 baz 777 777
Post a Comment for "Summarize Grouped Data In Pandas While Filtering After The Group Part"