Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

IQR to remove outlier

np.random.seed(33454)
stepframe = pd.DataFrame({'a': np.random.randint(1, 200, 20), 
                          'b': np.random.randint(1, 200, 20),
                          'c': np.random.randint(1, 200, 20)})

stepframe[stepframe > 150] *= 10
print (stepframe)

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

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

print (df)
      a    b     c
1   109   50   124
3   137   60  1990
4    19  138   100
5    86   83   143
6    55   23    58
7    78  145    18
8   132   39    65
9    37  146  1970
13   67  148  1880
15  124  102    21
16   93   61    56
17   84   21    25
19   34   52   126
Comment

Remove outlier using IQR

def remove_outlier(df_in, col_name):
q1 = df_in[col_name].quantile(0.25)
q3 = df_in[col_name].quantile(0.75)
iqr = q3-q1 #Interquartile range
fence_low  = q1-1.5*iqr
fence_high = q3+1.5*iqr
df_out = df_in.loc[(df_in[col_name] > fence_low) & (df_in[col_name] < fence_high)]
return df_out

re_dat = remove_outlier(stepframe, stepframe.columns)
Comment

PREVIOUS NEXT
Code Example
Python :: python if statements 
Python :: *args in python 
Python :: download youtube video by python 
Python :: sort list of list of dictionaries python 
Python :: change value in dataframe 
Python :: python label 
Python :: python parse xml string 
Python :: panda 
Python :: show post id on django admin interface 
Python :: remove a columns in pandas 
Python :: python while true 
Python :: math in python 
Python :: django class based views listview 
Python :: python chatbot error 
Python :: prime numbers 1 to 100 
Python :: How to perform heap sort, in Python? 
Python :: pandas columns 
Python :: how to inherit a class in python 
Python :: python how to find the highest even in a list 
Python :: python list max value 
Python :: def rectangles 
Python :: python press any key to continue 
Python :: python run uvicorn 
Python :: dictionaries in python 
Python :: tkinter filedialog filename 
Python :: Numpy split array into chunks of equal size 
Python :: mro in python 
Python :: how to use underscore in python 
Python :: put grid behind matplotlib 
Python :: create period pandas 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =