将两个 Pandas 列的布尔值与条件进行比较

Compare two Pandas columns of booleans with conditionals

我有一个数据框:

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

我想创建一个新列,如果布尔值相等,则 returns False,如果不同,则 returns 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

谢谢。

使用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