Remove rows on quartile deviation low or high

576 Views Asked by At

Wrote a code

df=data.copy()
for column in data.columns:
    Q1= np.quantile(data[column],0.25)
    Q3= np.quantile(data[column],0.75)
    IQR = Q3-Q1
    Low = Q1 - 3*(IQR)
    High = Q3 + 3*(IQR)
    df = df[(df[column] > Low ) | (df[column] < High)][column]

How to write the code so that it only accepts what's in the range. The code is showing an error.

----> 9     df = df[(df[column] > Low ) | (df[column] < High)][column]
1

There are 1 best solutions below

0
Suraj Goswami On

Yes, my logic was wrong. This one worked.

df=data.copy()
for column in data.columns:
    Q1= np.quantile(data[column],0.25)
    Q3= np.quantile(data[column],0.75)
    IQR = Q3-Q1
    Low = Q1 - 3*(IQR)
    High = Q3 + 3*(IQR)
    df = df[df[column].between(Low, High)]