how to use pandas filter with IQR?

前端 未结 6 1022
迷失自我
迷失自我 2020-12-28 13:17

Is there a built-in way to do filtering on a column by IQR(i.e. values between Q1-1.5IQR and Q3+1.5IQR)? also, any other possible generalized filtering in pandas suggested

6条回答
  •  没有蜡笔的小新
    2020-12-28 14:09

    Find the 1st and 3rd quartile using df.quantile and then use a mask on the dataframe. In case you want to remove them, use no_outliers and invert the condition in the mask to get outliers.

    Q1 = df.col.quantile(0.25)
    Q3 = df.col.quantile(0.75)
    IQR = Q3 - Q1
    no_outliers = df.col[(Q1 - 1.5*IQR < df.BMI) &  (df.BMI < Q3 + 1.5*IQR)]
    outliers = df.col[(Q1 - 1.5*IQR >= df.BMI) |  (df.BMI >= Q3 + 1.5*IQR)]
    

提交回复
热议问题