Search code examples
pythonpandasnumpyfillnalinspace

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


I have a column with the values changing from 0 to 600 and I want to group that values from 0 to 9.2 by 0.4 increments and 1 group between 9.2 and 600 values as outlier.I tried the following code ;

bin_labels = ['0-0.4', '0.4-0.8', '0.8-1.2', '1.2-1.6',
          '1.6-2.0', '2.0-2.4','2.4-2.8', '2.8-3.2',
          '3.2-3.6', '3.6-4.0','4.0-4.4', '4.4-4.8',
          '4.8-5.2', '5.2-5.6','5.6-6.0', '6.0-6.4',
          '6.4-6.8', '6.8-7.2','7.2-7.6', '7.6-8.0',
          '8.0-8.4', '8.4-8.8','8.8-9.2']

bins = np.linspace(0.0,9.2,24)

df['A_group'] = pd.cut(df['A'], bins = bins, labels = bin_labels, include_lowest = True)

After that I want to fill the values between 9.2 and 600 with '9.2-more' label value using following code ;

df['A_group'] = df['A_group'].fillna('9.2-more')   

But it says following error ;

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


Solution

  • You can append float("inf") to the bins and include "9.2-more" in the bin_labels:

    bin_labels = [  '0-0.4', '0.4-0.8', '0.8-1.2', '1.2-1.6',
                  '1.6-2.0', '2.0-2.4', '2.4-2.8', '2.8-3.2',
                  '3.2-3.6', '3.6-4.0', '4.0-4.4', '4.4-4.8',
                  '4.8-5.2', '5.2-5.6', '5.6-6.0', '6.0-6.4',
                  '6.4-6.8', '6.8-7.2', '7.2-7.6', '7.6-8.0',
                  '8.0-8.4', '8.4-8.8', '8.8-9.2', "9.20-more"]
    
    bins = np.append(np.linspace(0.0, 9.2, 24), float("inf"))
    df["A_group"] = pd.cut(df['A'], bins = bins, labels = bin_labels, include_lowest = True)