Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas plot value counts barplot in descending manner [duplicate]

I have a dataframe where i am trying to count the occurrence of each value. I plot it as horizontal bar but cant get it to be sorted.

df = pd.DataFrame(['A','A','A','B','B','C'],columns = ['letters'])

df.value_counts()

A 3
B 2
C 1

enter image description here

How can i get it sorted in a descending manner?

like image 914
jxn Avatar asked Mar 01 '18 22:03

jxn


2 Answers

You can do it by changing your plotting line like this

df.letters.value_counts().sort_values().plot(kind = 'barh')
like image 123
A.Gharbi Avatar answered Oct 24 '22 12:10

A.Gharbi


This might count as a bit of a hack, but try this:

df.letters.value_counts().sort_index(ascending=False).plot(kind='barh')
like image 31
Peter Leimbigler Avatar answered Oct 24 '22 13:10

Peter Leimbigler