I have a model which has many categorical variables. For each categorical variable there are many levels, like 50~. But not all of them have significant counts. I got these counts using the function value_counts() in Python:A                 50 B                 38C                 26D                 18E                 10...T                 1X                 1Z                 1How can I change the levels with count (say) less than 5 to a new level others?for x in data.class:    if x.value_counts() <30:        x = others
How to assign a new level to many levels of a categorical variable
python;categorical data
Using the notation you gave (i.e. data is the dataframe and data.class is the categorical variable you want to process), this can be done this way:def cut_levels(x, threshold, new_value):    value_counts = x.value_counts()    labels = value_counts.index[value_counts < threshold]    x[np.in1d(x, labels)] = new_valuecut_levels(data.class, 30, 'others')Note that this modifies the original data, thus if you want to keep it, do a copy of it before or change the code to this:def cut_levels(x, threshold, new_value):    x = x.copy()    value_counts = x.value_counts()    labels = value_counts.index[value_counts < threshold]    x[np.in1d(x, labels)] = new_value    return xnew_class = cut_levels(data.class, 30, 'others')