python——盖帽法进行数据清洗

 引言

      哈喽,各位小可爱,好久不见啦,小编最近比较忙,今天抽出时间来跟各位小可爱聊一聊python批量出来Excel异常数据相关的内容。

 理论

       在我们处理数据过程中难免会遇到异常值的情况,这个时候如果能够对长时间序列的Excel文件进行批处理可谓是省心又省力鸭,下面一起来学习吧。我们尝试用盖帽法进行处理,用第99百分位数替换前百分之一的异常高值,代码如下:

import xlrd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import numpy as np

path='Excel文件路径'
excel_features=os.listdir(path)#获取文件夹下的文件名称
out_path='处理后的文件输出路径'
#print(excel_features)

for excel_feature in excel_features:
    path_excel_feature=str(path+'/'+excel_feature)
    out_path_excel_feature=str(out_path+'/'+excel_feature)
    #print(out_path_excel_feature)
    #print(path_excel_feature)
    df_excel_feature=pd.read_excel(path_excel_feature)
    #print(df_excel_feature)
    #print('before \n',df_excel_feature['nh3'].describe())
    NH3=df_excel_feature['nh3']
    NH3_quantile99=NH3.quantile(0.99)#获取NH3列的99百分位数
    print(NH3_quantile99)
    df_excel_feature.loc[NH3>NH3_quantile99,'nh3']=NH3_quantile99#盖帽法处理异常值,异常值用995分位数进行替换
    df_excel_feature.to_excel(out_path_excel_feature,index=False)#处理后的DataFrame保存到Excel表格
    #print('after \n', df_excel_feature['nh3'].describe())
    print(excel_feature,'is cleaned successfull!')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风“流泪”了

您的鼓励将是我创作的不竭动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值