Skip to content

Instantly share code, notes, and snippets.

@cobanov
Last active May 14, 2019 14:09
Show Gist options
  • Save cobanov/a8d46c59ca332b01736cdca3b763000c to your computer and use it in GitHub Desktop.
Save cobanov/a8d46c59ca332b01736cdca3b763000c to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
d = {'col1': [1, 2, 3, 4, 5, 6, 100], 'col2': [ 2, 4, 6, 8, 10, 12, 200]}
df = pd.DataFrame(d)
print(df)
def detect_outlier(df, threshold):
outliers = []
means = []
stds = []
for i in range(0,len(df.columns)):
means.append(np.mean(df.iloc[:,i]))
stds.append(np.std(df.iloc[:,i]))
for df_value in df.iloc[:,i]:
z_score = (df_value - means[i]) / stds[i]
if np.abs(z_score) > threshold:
outliers.append(df_value)
return outliers, means, stds
print(detect_outlier(df, threshold = 2))
@sbasarans
Copy link

Thank you Mr. Cobanov !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment