0
votes

I have a pandas dataframe (df) and I need to sort it based on the counts of the values of a column. The values of the column are string.

For example, the target column's values are orange, apple, banana, peach. The individual counts ( df['fruit'].value_counts() ) are:

  • banana 2678
  • peach 2250
  • orange 1765
  • apple 1691

The result I need the initial dataframe (with all the columns etc) sorted based on these counts. So, in the first 2678 rows the value in the fruit column should be banana etc

1

1 Answers

1
votes

you can use map then sort_values for what you want.

df['sort_col'] = df['fruit'].map(df['fruit'].value_counts())
df = df.sort_values(by='sort_col', ascending=False).drop('sort_col', axis=1)