I have a DataFrame that have multiple columns and I want to filter all the rows that have an outlier values on at least 3 or more columns for each row . how can I do that?
I have used the following dataframe filtering method:
df[df.apply(lambda x: np.abs(x - x.mean()) / x.std() < 3).all(axis=1)
but it filters rows even when only single column have outlier value because of the.all() function.
We can get the sum of booleans on the row and select those with > 3
:
m = (df - df.mean()).abs().div(df.std()) < 3
df[m.sum(axis=1) > 3]
Note: we don't need apply here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.