【问题标题】:How to sum list of pandas boolean series如何总结熊猫布尔系列的列表
【发布时间】:2022-06-27 20:50:11
【问题描述】:

我在 pandas 中有超过 2 个布尔系列。

当我使用 & 或 +

search[0] & search[1]

我正确地得到了一个布尔系列

search[0] & search[1]
0     True
1    False

但我需要 sum(search) 之类的东西,它返回一个数字系列。

sum(search)
0    2
1    0

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

    您可以将系列之一转换为整数:

    s1 = pd.Series([True, False, False, True])
    s2 = pd.Series([True, False, True, False])
    
    s1.astype(int)+s2
    

    输出:

    0    2
    1    0
    2    1
    3    1
    dtype: int64
    

    【讨论】:

      猜你喜欢
      • 2018-05-14
      • 2019-01-14
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多