Alexander
Alexander

Reputation: 3

Find the another way to use groupby in pandas, python

I have the following table:

enter image description here

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:

enter image description here

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

Answers (1)

mozway
mozway

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

Related Questions