I have the following data frame:
df = pd.DataFrame({
'fruit':
['peaches']*5 + ['apples']*5 + ['bananas']*3 +
['nectarines']*3 + ['carrots']*3 + ['apricots']
})
And I would like to get output sorted first by the value count, then alphabetically by the name of the fruit:
apples 5
peaches 5
bananas 3
carrots 3
nectarines 3
apricots 1
I found this answer, but it looks out of date.
Seems like just using value_counts
will yield the result
df.fruit.value_counts()
Out[818]:
apples 5
peaches 5
bananas 3
carrots 3
nectarines 3
apricots 1
Name: fruit, dtype: int64
Update
df.fruit.value_counts().sort_index(ascending=False).sort_values(ascending=False)
apples 5
peaches 5
bananas 3
carrots 3
nectarines 3
apricots 1
Name: fruit, dtype: int64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With