相关疑难解决方法(0)

使用“OR”在熊猫中选择数据

我有一个值的数据框,我想探索异常值的行。我在下面写了一个可以用该groupby().apply()函数调用的函数,它适用于高值或低值,但是当我想将它们组合在一起时,我会产生一个错误。我以某种方式搞乱了布尔OR选择,但我只能找到使用&. 任何建议,将不胜感激。

扎克

df = DataFrame( {'a': [1,1,1,2,2,2,2,2,2,2], 'b': [5,5,6,9,9,9,9,9,9,20] } )

#this works fine
def get_outliers(group):
    x = mean(group.b)
    y = std(group.b)
    top_cutoff =    x + 2*y
    bottom_cutoff = x - 2*y
    cutoffs = group[group.b > top_cutoff]
    return cutoffs

#this will trigger an error
def get_all_ outliers(group):
    x = mean(group.b)
    y = std(group.b)
    top_cutoff =    x + 2*y
    bottom_cutoff = x -2*y
    cutoffs = group[(group.b > top_cutoff) or (group.b < top_cutoff)]
    return cutoffs …
Run Code Online (Sandbox Code Playgroud)

python pandas

4
推荐指数
1
解决办法
5274
查看次数

标签 统计

pandas ×1

python ×1