我有一个值数据框,我想探索异常值的行。我在下面编写了一个可以用该函数调用的groupby().apply()
函数,它适用于高值或低值,但是当我想将它们组合在一起时会产生错误。我以某种方式弄乱了布尔OR
选择,但我只能使用&
. 任何建议,将不胜感激。
扎克cp
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
#works fine
grouped1 = df.groupby(['a']).apply(get_outliers)
#triggers error
grouped2 = df.groupby(['a']).apply(get_all_outliers)