Home > Software engineering >  reset_index() not working for multiindex dataframe - 'cannot insert an item into a CategoricalI
reset_index() not working for multiindex dataframe - 'cannot insert an item into a CategoricalI

Time:03-11

I'm using this code to get to this dataframe:

df=df[['rank','Grade','search']].groupby(['Grade','rank']).count()
df2=df.div(df.groupby(['Grade']).transform('sum'))
df3=df2.unstack()

| search   | 
| rank     |  1  |  2  |  3  |  4  |  5  | 5-10 | 10-20 | 20  |
| Grade ---|-----|-----|-----|-----|-----|------|-------|-----|                         
| A        | 0.2 | 0.2 | 0.2 | 0.1 | 0.04| 0.04 | 0.01  | NaN |
| B        | 0.3 | 0.2 | 0.1 | 0.2 | 0.03| 0.05 | 0.2   | NaN |

Now, to plot a barplot for Grade, I would normally do reset_index() on the unstacked table. However if I do it now I get this error:

TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category

I have tried all sorts of things and I can't fix it. It's strange because it works for another table with the same exact format only opposite (i.e grouped by rank first and then grade)

CodePudding user response:

You can convert categorical to strings:

df3 = df_grade2.unstack().rename(columns=str).reset_index()
  • Related