Reputation: 3
I have the following table:
I need to merge 'completed' and 'partialy_completed' into one group. Let's say I call it "Ok" group. Then need to group by status and count percentage. How can I achieve that? Final result should be like this:
I can filter out values and add new column like:
mask = (df['state'] == 'completed') | (df['state'] == 'partialy_completed')
df['status2'][mask] = 'ok'
then use groupby. But is there better way to achieve that?
Upvotes: 0
Views: 29
Reputation: 261870
Here is a working solution:
(df.replace({'status': {'completed': 'OK',
'partially_completed': 'OK'}
})
.groupby('status')
.agg('count')
.assign(**{'%': lambda x: (x/x.sum()*100).astype(int)})
)
input:
status count
0 completed 2
1 partially_completed 3
2 expired 2
3 delayed 1
output:
count %
status
OK 2 50
delayed 1 25
expired 1 25
Upvotes: 1