Reputation: 2495
I have a Dataframe by the name bids_data
bids_data:
Supplier_ID shiper_RFQ
----------
0 2305 5000
1 2309 5200
2 2305 6500
3 2307 4500
4 2301 900
5 2302 10000
6 2306 4500
and I want to remove the outliers rows from shiper_RFQ and store them in another dataframe. I tried converting the shiper_RFQ in a list and then finding the outliers but it doesn't work well.
Upvotes: 2
Views: 809
Reputation: 5324
if you have good data then use threshold = 0.5
threshold = 1
print(df[df['shiper_RFQ'].apply(lambda x: np.abs(x - df['shiper_RFQ'].mean()) / df['shiper_RFQ'].std() < threshold)])
also this
df = df[ np.abs(df['shiper_RFQ'] - df['shiper_RFQ'].mean()) / df['shiper_RFQ'].std() < threshold]
both will have same result
output
Supplier_ID shiper_RFQ
0 2305 5000
1 2309 5200
2 2305 6500
3 2307 4500
6 2306 4500
if you print you can see the anomaly
print(df['shiper_RFQ'].apply(lambda x: np.abs(x - df['shiper_RFQ'].mean()) / df['shiper_RFQ'].std()))
0 0.084182
1 0.010523
2 0.468261
3 0.268329
4 1.594192
5 1.757294
6 0.268329
Upvotes: 1
Reputation: 3290
You can identify outliers by finding rows that differ from the mean column value by more than 1.5 standard deviations (or any other cut-off value you choose):
df['outliers'] = 0
df.loc[(df.shiper_RFQ - df.shiper_RFQ.mean()).abs() > 1.5*df.shiper_RFQ.std(), 'outliers'] = 1
print(df)
Supplier_ID shiper_RFQ outliers
0 2305 5000 0
1 2309 5200 0
2 2305 6500 0
3 2307 4500 0
4 2301 900 1
5 2302 10000 1
6 2306 4500 0
Then you can store them in another data frame and remove them from the original:
df2 = df[df.outliers == 1].reset_index(drop=True)
df = df[df.outliers == 0].reset_index(drop=True)
print(df2)
print(df)
Supplier_ID shiper_RFQ outliers
0 2301 900 1
1 2302 10000 1
Supplier_ID shiper_RFQ outliers
0 2305 5000 0
1 2309 5200 0
2 2305 6500 0
3 2307 4500 0
4 2306 4500 0
Upvotes: 1