remove outlier python code example

Example 1: delete outliers in pandas

cols = ['col_1', 'col_2'] # one or more

Q1 = df[cols].quantile(0.25)
Q3 = df[cols].quantile(0.75)
IQR = Q3 - Q1

df = df[~((df[cols] < (Q1 - 1.5 * IQR)) |(df[cols] > (Q3 + 1.5 * IQR))).any(axis=1)]

Example 2: outliers removal

#Removing outliers first then skewness
from scipy.stats import zscore
z=abs(zscore(df))
print(z.shape)
df=df[(z<3).all(axis=1)]
df.shape

Example 3: delete outliers in pandas

Q1 = df.quantile(0.25)
Q3 = df.quantile(0.75)
IQR = Q3 - Q1

df = df[~((df < (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)]