3

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.

cbare
  • 12,060
  • 8
  • 56
  • 63
Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
  • Well the problem is that if there are no `F`s, then these can not be counted. Pandas simply does not know that something like an `F` exists. – Willem Van Onsem Dec 24 '19 at 08:18
  • [Categorical](https://pandas.pydata.org/docs/reference/api/pandas.Categorical.html) variables would work here. See the [Categorical data Operations](https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html#operations) section of the Pandas User's Guide. – cbare May 11 '23 at 09:48

2 Answers2

5

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')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

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()
Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40