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

时间:2018-09-04 02:22:21

标签: python pandas dataframe boolean boolean-logic

我有一个数据框:

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

谢谢。

1 个答案:

答案 0 :(得分: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
相关问题