4

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!

Murtuza_07
  • 55
  • 1
  • 7

1 Answers1

6

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']]
To view all columns:
df.assign(freq=df.groupby('Fruits')['Fruits'].transform('count'))\
  .sort_values(by=['freq','Fruits'],ascending=[False,True])
Sadra
  • 2,480
  • 2
  • 20
  • 32
Allen Qin
  • 19,507
  • 8
  • 51
  • 67
  • 1
    This works but I have a multi-column dataframe so how can I display all columns? – mLstudent33 Mar 14 '20 at 01:01
  • 1
    For anyone referencing this post in the future, in response to mLstudent33, to display all columns with the first method (the method for which the answer was accepted), you would just get rid of ".loc[:,['Fruits']]" – Erin Sep 15 '20 at 21:31