How do I group by based on a column in pandas data frame in python and in descending order? (Jupyter Notebook)

Viewed 147

I have a data frame with columns, user_id and app_names, need to find the number of users of each app(basically a group by based on app_names with descending order of the number of users).

Data I have:

user_id   app_names

101       whatsapp
101       fb
102       fb 
102       instagram
103       fb
103       whatsapp

What I want:

app_names   num_users

fb          3
whatsapp    2
instagram   1

Any Suggestions.
1 Answers

Try this code:

ss = df.groupby('app_names')['user_id'].count().reset_index(name = 'num_users')
ss.sort_values(by = ['num_users'], inplace = True, ascending = False)
print(ss)

the first line would get the count. the second line would sort based no the num_users columns in descending order.

Related