1

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.

Scott
  • 122
  • 1
  • 10
  • 2
    Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – APhillips Jan 08 '20 at 17:26
  • 1
    `(df['col1']>=df['col2']).astype(int)` – anky Jan 08 '20 at 17:26

1 Answers1

3

You could use:

df['col3'] = df.col1.ge(df.col2).view('i1')

print(df)

    col1  col2  col3
0     1     3     0
1     5     1     1
2     7     4     1
3     5    10     0
yatu
  • 86,083
  • 12
  • 84
  • 139