Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Pandas – rename a value conditionally

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading