[英]How do I group by based on a column in pandas data frame in python and in descending order? (Jupyter Notebook)
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).我有一个包含 user_id 和 app_names 列的数据框,需要找到每个应用程序的用户数(基本上是一个基于 app_names 的组,按用户数降序排列)。
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.
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.
第二行将根据 num_users 列按降序排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.