how to sort values of pandas dataframe for iqr 1

how to sort values of pandas dataframe for iqr

    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]


Here is what the above code is Doing:
1. It takes in a dataframe and returns a dataframe.
2. It first copies the dataframe and then gets the numeric data from the original dataframe.
3. It then calculates the first and third quartiles and the interquartile range.
4. It then calculates the lower and upper bounds.
5. It then iterates through the columns and rows and replaces the values that are less than the lower bound with the lower bound and the values that are greater than the upper bound with the upper bound.
6. It then replaces the columns in the copied dataframe with the columns from the original dataframe.
7. It then returns the copied dataframe.

Similar Posts