【问题标题】:Logical Operation On a Few Columns PandasPandas 对几列的逻辑运算
【发布时间】:2021-05-14 20:13:23
【问题描述】:

我正在尝试在 Pandas 中创建一个列,以查看所选列集中的任何列是否具有 False 值。

Index Item 1 Truth Item 1 Value Item 2 Truth Item 2 Value
First True 65 True 10
Second False 50 True 55

“Truth”列不止 2 个,因此列表理解等在这里会非常有帮助。第 1 行的输出应该为真,第 2 行的输出应该为假。到目前为止,我已经尝试过使用 .apply 和 all() 等,但到目前为止似乎没有任何效果。

谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以.filter 数据框并将.all() 与axis=1(行)一起使用:

    df["result"] = df.filter(regex=r"Truth$").all(axis=1)
    print(df)
    

    打印:

        Index  Item 1 Truth  Item 1 Value  Item 2 Truth  Item 2 Value  result
    0   First          True            65          True            10    True
    1  Second         False            50          True            55   False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      相关资源
      最近更新 更多