Sort by Frequency of Values in a Column - Pandas

对着背影说爱祢 提交于 2021-02-09 02:58:31

问题


I have a column in a dataframe

Fruits
Apple
Mango
Banana
Apple
Mango
Banana
Apple
Mango
Grapes

I want to sort this column by Frequency of the values occurring in it, So the dataframe now should be:

Fruits
Apple
Apple
Apple
Banana
Banana
Banana
Mango
Mango
Grapes

Thanks!


回答1:


Create a freq column and then sort by freq and fruit name.

df.assign(freq=df.apply(lambda x: df.Fruits.value_counts()\
  .to_dict()[x.Fruits], axis=1))\
  .sort_values(by=['freq','Fruits'],ascending=[False,True]).loc[:,['Fruits']]
Out[593]: 
   Fruits
0   Apple
3   Apple
6   Apple
1   Mango
4   Mango
7   Mango
2  Banana
5  Banana
8  Grapes

A similar approach by using groupby and count:

df.assign(freq=df.groupby('Fruits')['Fruits'].transform('count'))\
  .sort_values(by=['freq','Fruits'],ascending=[False,True]).loc[:,['Fruits']]


来源:https://stackoverflow.com/questions/44363585/sort-by-frequency-of-values-in-a-column-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!