我有一张 table df
像这样,但更长,还有许多其他 type
值。
类型
重量
一种
35.1
一种
36.7
乙
100.2
乙
99.3
乙
102.0
乙
5.0
一种
38.2
一种
250.8
我想从 df
中删除所有使用第 95 个百分位数的异常值但在 type
中分解为单个值的记录柱子。
对于类型的单个值,我这样做:
my_perc = 95
temp = df[df['type'] == 'a']
temp[temp.weight < np.percentile(temp.weight, my_perc)]
现在我想为整个表自动执行此操作 df
,考虑到 type
中的个别群体柱子。我也试过这个:
df[df.groupby(['type'])['weight'] < np.percentile(df.weight, my_perc)]
但它不起作用。你有什么想法吗?
最佳答案
好的,可能问题解决了:
my_perc = 0.95
df[df.groupby('type')['weight'].transform(lambda x : x < x.quantile(my_perc))]
关于python - 如何根据百分位数从组中删除异常值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68998189/