Pandas conditional comparison: based on multiple columns

て烟熏妆下的殇ゞ 提交于 2020-12-13 03:31:40

问题


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.


回答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


来源:https://stackoverflow.com/questions/62603518/pandas-conditional-comparison-based-on-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!