代码之家  ›  专栏  ›  技术社区  ›  dia

在筛选数据帧行时,条件是单独工作的,而不是一起工作的[值错误:序列的真值不明确]

  •  1
  • dia  · 技术社区  · 8 年前

    我再次尝试在时间条件下挑出某些行来计算单独时间段的平均值/标准值。

    file = pd.read_csv('test/Res/1002', sep='\t', encoding = 'utf-8')
    
    print(file['hm']==47)
    print(file['hm']==48)
    print(1<=file['hm']<=14)
    

    我得到正确计算的布尔值的真/假列表。但在以下情况下,请接收此信息-> ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

    overn_std = file[(file['hm'] == 47)| (file['hm'] == 48) | (1 <= file['hm'] <= 14) ]
    

    enter image description here

    过去,我通过替换条件解决了相同的问题 or 带|。 hm列的数据类型为int。

    1 回复  |  直到 8 年前
        1
  •  1
  •   jezrael    8 年前

    我想需要分开 (1 <= file['hm'] <= 14) 链接到2个单独的条件 & 对于 AND :

    overn_std = file[(file['hm'] == 47) | 
                     (file['hm'] == 48) | 
                     ((file['hm'] >= 1) & (file['hm'] <= 14)) ]
    

    也可以分别创建每个遮罩:

    m1 = (file['hm'] == 47)
    m2 = (file['hm'] == 48)
    m3 = (file['hm'] >= 1)
    m4 = (file['hm'] <= 14)
    
    overn_std = file[m1 | m2 | (m3 & m4 )] 
    

    更好的是使用 isin 具有 between :

    overn_std = file[(file['hm'].isin([47,48])) | (file['hm'].between(1,14)) ]
    

    样品 :

    file = pd.DataFrame({
        'hm': range(50)
    })
    
    
    overn_std = file[(file['hm'].isin([47,48])) | (file['hm'].between(1,14)) ]
    print (overn_std)
        hm
    1    1
    2    2
    3    3
    4    4
    5    5
    6    6
    7    7
    8    8
    9    9
    10  10
    11  11
    12  12
    13  13
    14  14
    47  47
    48  48