1categorical_features=[feature for feature in dataset.columns if dataset[feature].dtype=='O']
2for feature in categorical_features:
3 temp=dataset[feature].value_counts(normalize=True)
4 temp_df=temp[temp>0.01].index
5
6 dataset[feature]=np.where(dataset[feature].isin(temp_df),dataset[feature],'Rare_var')
7 # condition satisfies then 'X' else'Y'
8 #condition----------> ,take this values else,'Rare_var'