Python Pandas:如何去除列中的异常值,并用先前的值替换它们(假设它们不是异常值)?

Python Pandas: How to remove the outliers in a column, and replace them with prior values (assuming they are not outlier)?

我有一个如下所示的数据框:

Date | Value. 
2020-03-18 10 
2020-03-19 20 
2020-03-20 30 
2020-03-21 25 
2020-03-22 35 
2020-03-23 50 
2020-03-24 1900000 
2020-03-25 1200000 
2020-03-26 50 
2020-03-27 25 
2020-03-28 35 
2020-03-29 50 
2020-03-30 25 
2020-03-31 35 
... 
2021-01-31 45 

用先前的非离群值替换 value 列中的离群值的最快方法是什么?

我需要注意不要简单地用先前的数字替换异常值,因为如果先前的值也是异常值(如 2020-03-24 中所示),它会搞砸。

非常感谢您的帮助!

如评论所述,您的示例数据没有异常值。但是,按照你的逻辑,你可以这样做:

std, mean = df['Value'].agg(['std','mean'])

df['Value'] = df['Value'].where(df['Value'].between(mean-3*std, mean+3*std)).ffill()