Решение должно быть упрощено с normalize=True
в Series.value_counts
:
def replace_thresh(df, col, thresh, new_val):
s = df[col].value_counts(normalize=True).mul(100)
df[col] = np.where(df[col].isin(s.index[s < thresh]), new_val, df[col])
return df
df = replace_thresh(df, 'col', 1, 'Other')