【问题标题】:Compare two Pandas columns of booleans with conditionals比较两个带有条件的 Pandas 布尔值列
【发布时间】:2019-02-08 23:54:56
【问题描述】:

我有一个数据框:

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】:

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

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      相关资源
      最近更新 更多