如何从数据帧中删除异常值?

2024-06-01 02:56:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个(268X4)df,找到了一列的离群值(22,1)。我想把那些异常值从df中去掉。我该怎么做?在

> 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)] 

Tags: thetoimportdfforasnpfirst