Khalil Al Hooti
Khalil Al Hooti

Reputation: 4546

Include zeros in value_counts pandas categorial data

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.

Upvotes: 4

Views: 1409

Answers (2)

Khalil Al Hooti
Khalil Al Hooti

Reputation: 4546

This worked for me.

new_index = sorted(set([x[0] for x in df1['new'].cat.categories]), reverse=True)
counts_gardes_group = df1['new'].str[0].value_counts(sort=False).reindex(
     new_index, fill_value=0
)

counts_gardes_group.plot(kind='bar')

plt.show()

Upvotes: 0

jezrael
jezrael

Reputation: 863541

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

Upvotes: 6

Related Questions