【问题标题】:Python Pandas combine multiple boolean seriesPython Pandas 结合了多个布尔系列
【发布时间】:2022-06-22 20:28:24
【问题描述】:

我有一个包含多个列的数据框,如下所示

gender marital education
male single tertiary

我有一个包含布尔系列的需求列表,我需要使用 & 符号将它们全部组合起来。

bool_gender = df["gender"] == "male"
bool_marital = df["marital"] == "married"
bool_education = df["education"] == "secondary"
[bool_gender, bool_marital, bool_education]

如何使用 Python 3 中的函数式编程组合列表中的所有项目,以获得一个布尔值,该值是以下表达式的结果:

desired output = bool_gender & bool_marital & bool_education


Possible use: 

reduce("&", map(function, iter))


【问题讨论】:

    标签: python pandas list functional-programming


    【解决方案1】:

    您可以使用reduce 版本的numpy.bitwise_and

    np.bitwise_and.reduce([bool_gender, bool_marital, bool_education])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-17
      • 2012-10-08
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多