how to use pandas filter with IQR?

前端 未结 6 870
迷失自我
迷失自我 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:03

    You can try using the below code, also, by calculating IQR. Based on the IQR, lower and upper bound, it will replace the value of outliers presented in each column. this code will go through each columns in data-frame and work one by one by filtering the outliers alone, instead of going through all the values in rows for finding outliers.

    Function:

        def mod_outlier(df):
            df1 = df.copy()
            df = df._get_numeric_data()
    
    
            q1 = df.quantile(0.25)
            q3 = df.quantile(0.75)
    
            iqr = q3 - q1
    
            lower_bound = q1 -(1.5 * iqr) 
            upper_bound = q3 +(1.5 * iqr)
    
    
            for col in col_vals:
                for i in range(0,len(df[col])):
                    if df[col][i] < lower_bound[col]:            
                        df[col][i] = lower_bound[col]
    
                    if df[col][i] > upper_bound[col]:            
                        df[col][i] = upper_bound[col]    
    
    
            for col in col_vals:
                df1[col] = df[col]
    
            return(df1)
    

    Function call:

    df = mod_outlier(df)
    

提交回复
热议问题