简体   繁体   English

如何根据python中的pandas数据框中的列按降序分组? (Jupyter 笔记本)

[英]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.

相关问题 如何在 Jupyter 笔记本中使用 Python 和 pandas 从特定列中提取数据? - How do I pull data from a specific column using Python and pandas in a Jupyter notebook? 如何按列的值对pandas数据帧的行进行分组? - How do I group the rows of a pandas data frame by a value of a column? 如何对 Pandas dataframe 进行条件排序(即一列中的升序和降序基于另一列中的值)? - How to do conditional sorting on a Pandas dataframe (i.e. ascending and descending order within one column based on values in another column)? 如何在 pandas 数据帧的特定列索引处插入列? (更改 pandas 数据帧中的列顺序) - how do I insert a column at a specific column index in pandas data frame? (Change column order in pandas data frame) 我可以在 vscode jupyter notebook 中更改 pandas 数据框的外观吗? - Can I change the look of pandas data frame in vscode jupyter notebook? python - 如何在python pandas中分组并取一列的计数除以数据框第二列的唯一计数? - How to do group by and take Count of one column divide by count of unique of second column of data frame in python pandas? 如何将一个现有列的值添加/合并到另一列 - Python - Pandas - Jupyter Notebook - How can I add/merge values from one existing column to another column - Python - Pandas - Jupyter Notebook Python-如何在列数据框名称pandas中重置数字顺序? - Python - how to reset the order of number in the column data frame name pandas? 如何识别 Python Pandas Data Frame 列中值的顺序? - How to recognize order of values in column in Python Pandas Data Frame? python:pandas - 数据框列的顺序 - python: pandas - order of data frame column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM