kee
kee

Reputation: 11629

DataFrame Plot: how to sort X axis

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?

Upvotes: 7

Views: 9101

Answers (1)

migjimen
migjimen

Reputation: 571

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')

Upvotes: 13

Related Questions