【问题标题】:How to use "and" with conditions in Python? [duplicate]如何在 Python 中将“and”与条件一起使用? [复制]
【发布时间】:2019-10-13 09:40:17
【问题描述】:

我正在尝试根据原始数据集中某个列的条件创建数据子集。它只适用于一个条件(例如,data[TNT]10000 和 data[TNT]

#group2= 10k<TNT<25k
group2_b =  data['TNT']>10000 and data['TNT']<=25000
group2 = data[group2_b]

错误信息是:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    尝试加括号:

    group2_b = (data['TNT']>10000) and (data['TNT']<=25000)
    

    或使用方法:

    group2_b = data['TNT'].gt(10000) & data['TNT'].le(25000)
    

    或使用between:

    group2_b = data['TNT'].between(10000,25000)
    

    【讨论】:

      猜你喜欢
      • 2013-12-12
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2021-10-14
      相关资源
      最近更新 更多