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

How to create feature with condition by using where()?

I am encoding my categorical data in column "Gender" by get_dummies()
Data at begining
Next, I have to create new feature isMale by using where()
This feature should show gender by value(0 and 1), if gender is "female" than isMale equals to 0, and analogical gender="male" than isMale=1

And this feature should swap my "Gender" or just concatenate to my dataset

I don’t get how to use where() method for creating feature

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 :

To replace your gender column:

import numpy as np
import pandas as pd


df['Gender'] = np.where(df['Gender'] == "Male", 1, 0)

To add a new column:

import numpy as np
import pandas as pd


df['isMale'] = np.where(df['Gender'] == "Male", 1, 0)
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