简体   繁体   English

如何从数据框中删除离群值?

[英]How to remove outliers from a dataframe?

I have a (268X4) df and found the outliers (22,1) for one column. 我有(268X4)df,发现一列的异常值(22,1)。 I want to remove those outliers from the df. 我想从df中删除那些离群值。 How do I do that? 我怎么做?

> df=df_nonull import pandas as pd   # to manipulate dataframes import
> numpy as np   # to manipulate arrays
> 
> # a number "a" from the vector "x" is an outlier if 
> # a > median(x)+1.5*iqr(x) or a < median-1.5*iqr(x)
> # iqr: interquantile range = third interquantile - first interquantile def 
>outliers(x): 
>        return np.abs(x- x.median()) > 1.5*(x.quantile(.75)-
>x.quantile(0.25))
> 
> # Give the outliers for the first column for example 
>outliers=df.StockValue[outliers(df.StockValue)] 

You can only remove the whole row, njot a single cell like (22,1). 您只能删除整行,njot像(22,1)这样的单个单元格。 If you want to remove the complete row of the data. 如果要删除数据的完整行。

df = df.drop(df.index[[22]]) df = df.drop(df.index [[22]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM