Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include zeros in value_counts pandas categorial data

Tags:

python

pandas

This is a follow-up question for a question I asked previously. The old question can be found here, I received an answer from @jezrael.

Now I want to plot the grades.

For plotting all grades I can do

counts_gardes = df1['new'].value_counts(sort=False)
counts_gardes.plot(kind='bar')

enter image description here

However, I could not figure out how to plot per grade group including zero counts.

counts_gardes_group = df1['new'].value_counts(sort=False)
counts_gardes_group.plot(kind='bar')

enter image description here

I would like to also include zero counts for F in the plotted figures. I tried the solutions provided in here, here and here but they did not work. The first returns all grades, while the latter gives an error stating that index does not have levels.

Any help is really appreciated.

like image 780
Khalil Al Hooti Avatar asked Oct 29 '25 10:10

Khalil Al Hooti


1 Answers

You can use Series.reindex with swap order (if necessary) of list of all grades and if not match replace to 0:

grade_leters = ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D','D-', 'F']

counts_gardes_group = (df1['new'].value_counts(sort=False)
                                 .reindex(grade_leters[::-1], fill_value=0))
counts_gardes_group.plot(kind='bar')
like image 181
jezrael Avatar answered Oct 31 '25 23:10

jezrael