7

I am plotting some counts from a field of dataframe (pandas) and I found that the X axis is sorted by the counts (descending order). Instead is it possible to sort by the alphabetical order of the field?

Here is the Python code:

df['cartype'].value_counts().plot(kind='bar')

This sorts by the count but I want to sort by the cartype names. Any solution?

1
  • 1
    df['cartype'].value_counts(sort=False).plot(kind='bar')? Commented Sep 10, 2019 at 17:18

1 Answer 1

13

One option is to use the function 'sort_index', which sorts Series by index labels, after the call to 'value_counts':

df['cartype'].value_counts().sort_index().plot(kind='bar')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.