I'm trying to set the values in a column of a df to either 0 or 1 based on the comparison of two other columns.
My initial df would be this:
col1 col2 col3
1 3 NaN
5 1 NaN
7 4 NaN
5 10 NaN
I'm using this code to try to set values in col3
to 1 where col1
is greater than col2
all other rows would be 0:
df[df['col1'] >= df['col2']]['col3'] = 1
df[df['col1'] < df['col2']]['col3'] = 0
This is for sure the wrong way to do it, but I'm not sure how else to approach it.
The desired result is a df with the following values:
col1 col2 col3
1 3 0
5 1 1
7 4 1
5 10 0
Thanks in advance.