比较两个带有条件的Poleas布尔列

问题描述 投票:1回答:1

我有一个数据帧:

df
     col1    col2
1    True    False
2    True    True
3    False   False
4    False   True

我想创建一个新列,如果布尔值相等,则返回False,如果它们不同,则返回True

就像是:

df['col3'] = False if df['col1'] == df['Col2'] else True

df
     col1    col2    col3    
1    True    False   True
2    True    True    False
3    False   False   False
4    False   True    True

谢谢。

python pandas dataframe boolean boolean-logic
1个回答
3
投票

使用ne不相等

df['New']=df.col1.ne(df.col2)
df
Out[140]: 
    col1   col2    New
1   True  False   True
2   True   True  False
3  False  False  False
4  False   True   True
© www.soinside.com 2019 - 2024. All rights reserved.