df2 = np.where(df2['color'] != 'blue' | 'red')
I want to create one category for many categorical values, such as:
If the color is not blue or red, call the color "other"
Please and thank you <3
df2 = np.where(df2['color'] != 'blue' | 'red')
I want to create one category for many categorical values, such as:
If the color is not blue or red, call the color "other"
Please and thank you <3
You are basically halfway there. You just have to provide 2 more parameters to achieve what you want.
df2['color'] = np.where((df2['color'] == 'blue') | (df2['color'] == 'red'), df2['color'], 'other')
Reading the equality is easier because there is less cognitive load. If the condition is True the df2['color']
will be selected. If the condition is false for that row 'other'
will be selected