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

change a numeric column to binary?

My problem is the following:
Create a binary variable in which:

𝑌=1 indicates that the house was sold for over $200,000
𝑌=0 indicates that the house was sold for less than or equal to $200,000

sac.loc[sac["price"]> 200000]= 1
sac.loc[sac["price"]<= 200000]= 0

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

It changes all the values ​​to 0 and I don’t know how to make that change.

>Solution :

You are overwriting the whole row, thus sac["price"] becomes 1 where was > 200000 and will be considered <200000 in the next step.

Use:

sac['col'] = sac["price"].gt(200000).astype(int)

Where 'col' is the name of the column in which you want to assign the "boolean" value.

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