4daJKong
4daJKong

Reputation: 2045

how to sort the result by pandas.value_counts

I want to count the number of student who got 0-5 grade in one dataset

I use this function final_grade_num =pd.value_counts(final_grade) to get a result like

4.0    487
3.0    432
2.0    376
5.0    334
1.0    232
0.0    139

however, I want to get a sorted list like

0.0 139, 1.0 232...5.0 334

because I need to use this dataset to draw a plot

plt.bar(range(0,6), final_grade_num)

Is there any method to change, I tried to use sorted method, but the result shows it depends on the number of students, not grade

Upvotes: 6

Views: 10725

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36604

value_counts() has a sort argument that defaults to True. Just set it to False and it will be sorted by value instead.

df['col'].value_counts(sort = False).plot.bar(title='My Title')

Or:

df['col'].value_counts().sort_index().plot.bar()

Upvotes: 10

Related Questions