pandas ValueError: Cannot setitem on a Categorical with a new category, set the categories first

We Are Going To Discuss About pandas ValueError: Cannot setitem on a Categorical with a new category, set the categories first. So lets Start this Python Article.

pandas ValueError: Cannot setitem on a Categorical with a new category, set the categories first

  1. How to solve pandas ValueError: Cannot setitem on a Categorical with a new category, set the categories first

    In your solution is problem there is categorical column, so if replace only some rows pandas want ouput column set to categoricals, and because 0,1 not exist in categories is raised error.
    Sample data with categorical column:
    df = pd.DataFrame({'decision':['Yes','No']}) df['decision'] = pd.Categorical(df['decision'])
    Solutions with Series.map and cat.rename_categories for categorical ouput:
    df['decision1'] = df['decision'].map({'Yes':1, 'No':0}) df['decision2'] = df['decision'].cat.rename_categories({'Yes':1, 'No':0})
    If only Yes and No values is possible recreate all values by compare by Yes and cast to integer for True, False to 1,0 mapping like mentioned @arhr, categorical is lost:
    df['decision3'] = (df['decision'] == 'Yes').astype(int) print (df) decision decision1 decision2 decision3 0 Yes 1 1 1 1 No 0 0 0 print (df.dtypes) decision category decision1 category decision2 category decision3 int32 dtype: object

  2. pandas ValueError: Cannot setitem on a Categorical with a new category, set the categories first

    In your solution is problem there is categorical column, so if replace only some rows pandas want ouput column set to categoricals, and because 0,1 not exist in categories is raised error.
    Sample data with categorical column:
    df = pd.DataFrame({'decision':['Yes','No']}) df['decision'] = pd.Categorical(df['decision'])
    Solutions with Series.map and cat.rename_categories for categorical ouput:
    df['decision1'] = df['decision'].map({'Yes':1, 'No':0}) df['decision2'] = df['decision'].cat.rename_categories({'Yes':1, 'No':0})
    If only Yes and No values is possible recreate all values by compare by Yes and cast to integer for True, False to 1,0 mapping like mentioned @arhr, categorical is lost:
    df['decision3'] = (df['decision'] == 'Yes').astype(int) print (df) decision decision1 decision2 decision3 0 Yes 1 1 1 1 No 0 0 0 print (df.dtypes) decision category decision1 category decision2 category decision3 int32 dtype: object

Solution 1

In your solution is problem there is categorical column, so if replace only some rows pandas want ouput column set to categoricals, and because 0,1 not exist in categories is raised error.

Sample data with categorical column:

df = pd.DataFrame({'decision':['Yes','No']})

df['decision'] = pd.Categorical(df['decision'])

Solutions with Series.map and cat.rename_categories for categorical ouput:

df['decision1'] = df['decision'].map({'Yes':1, 'No':0})
df['decision2'] = df['decision'].cat.rename_categories({'Yes':1, 'No':0})

If only Yes and No values is possible recreate all values by compare by Yes and cast to integer for True, False to 1,0 mapping like mentioned @arhr, categorical is lost:

df['decision3'] = (df['decision'] == 'Yes').astype(int)
print (df)
  decision decision1  decision2 decision3
0      Yes         1          1         1
1       No         0          0         0

print (df.dtypes)
decision     category
decision1    category
decision2    category  
decision3       int32
dtype: object

Original Author jezrael Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment