如何去除某一类数据中的异常值?

How to remove outliers in a certain category of data?

boxplot for the data

plot = sns.boxplot(y = 'charges', x = 'smoker' , data = df)

我试图在此处删除 "no" 类别的异常值。为此,我过滤了无值

no_charges = d[d.smoker == 'no'].charges

计算 IQR

Q1 = no_charges.quantile(0.25)
Q3 = no_charges.quantile(0.75)
IQR = Q3-Q1

过滤异常值

da = df.copy()
no = da[da.smoker == 'no']
rem = no[(no.charges > (Q3 + 1.5*IQR)) | (no.charges < (Q3 - 1.5*IQR))]

但是当我尝试从主 table

中删除所需的数据时,我遇到了问题
da.drop[[(no.charges > (Q3 + 1.5*IQR)) | (no.charges < (Q3 - 1.5*IQR))], inplace=True]

这样可以吗?如果不是那么如何过滤值?

去除异常值

df = da[~((no_charges < (Q1 - 1.5 * IQR)) |(no_charges > (Q3 + 1.5 * IQR))).any(axis=1)]

详细解释可以参考这个答案。