how to find iqr in pandas code example
Example 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]
return(df1)
Example 2: calculate iqr in python dataset example
def outlier_treatment(datacolumn): sorted(datacolumn) Q1,Q3 = np.percentile(datacolumn , [25,75]) IQR = Q3 — Q1 lower_range = Q1 — (1.5 * IQR) upper_range = Q3 + (1.5 * IQR) return lower_range,upper_range