【发布时间】:2012-11-14 09:15:45
【问题描述】:
我有一个值数据框,我想探索异常值的行。我在下面编写了一个函数,可以使用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)
【问题讨论】: