change a numeric column to binary?

Question:

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

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

Asked By: Jesus Bueno

||

Answers:

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.

Answered By: mozway