1

I have a df

   col1  col2  col3  col4  
0     1     2     3     4    
1     2     2     3     4    
2     3     4     3     5   
3     4     3     2     1   

And I want to add a new column based on:

if (col1 & col2) < (col3 & col4) --- > 2

I followed the approach similar to this post, just without max() as follow but all didn't work:

df[['col1','col2']] < df[['col3','col4']] 

(df['col1'] and df['col2']) < (df['col3'] and df['col4'])

What's the correct way to do it? Thanks.

saga
  • 736
  • 2
  • 8
  • 20

1 Answers1

1
mask = df[['col1','col2']].max(1) < df[['col3','col4']].min(1)

df['new_col'] = np.where(mask, 2, np.nan)

Output:

   col1  col2  col3  col4  new_col
0     1     2     3     4      2.0
1     2     2     3     4      2.0
2     3     4     3     5      NaN
3     4     3     2     1      NaN
halfer
  • 19,824
  • 17
  • 99
  • 186
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • just changed the first condition to max (instead of min), didn't think of it this way lol.... – saga Jun 26 '20 at 22:30
  • Hi Quang. Please refrain from adding "IIUC" to every answer if you can. There are several editing principles behind this: (1) it is redundant and reflexive, and does not add anything to the post; (2) it indicates some uncertainty on your part, and you may wish to clarify that in the comments before answering, and (3) where an answer is accepted, it removes the doubt about whether you understood it correctly. – halfer Oct 24 '20 at 20:37
  • With all of that in mind, this material would classify as (conversational) ["fluff" that we remove here](https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-when-editing-questions). Thanks! – halfer Oct 24 '20 at 20:38