数据分析第四篇:数据清洗

需要清洗的数据有下面几种形式

 

2.1错误值

出现大量0的话,可以使用缺失值替代,然后再用缺失值填补的方法处理

camp['AvgIncome']=camp['AvgIncome'].replace({0: np.NaN})

 

2.2 缺失值

 

vmean = camp['Age'].mean(axis=0, skipna=True)

camp['Age_empflag'] = camp['Age'].isnull()

camp['Age']= camp['Age'].fillna(vmean)

camp['Age'].describe()

 

2.3 重复

去掉重复值

 

2.4 数据不一致

- 时间单位不同可以使用正则使其一致化

- 金额单位不同需要一致化

 

2.5 离群值(异常值)

1.删除异常值(5倍标准差之外的数据)

2.盖帽法处理异常值,把1%的异常值用99%处的值代替

def blk(floor, root): # 'blk' will return a function

    def f(x):       

        if x < floor:

            x = floor

        elif x > root:

            x = root

        return x

    return f

 

q1 = camp['Age'].quantile(0.01) # 计算百分位数

q99 = camp['Age'].quantile(0.99)

blk_tot = blk(floor=q1, root=q99) # 'blk_tot' is a function

camp['Age']= camp['Age'].map(blk_tot)

camp['Age'].describe()

 

3.分箱法处理异常值

camp['Age_group1'] = pd.qcut( camp['Age'], 4) # 这里以age_oldest_tr字段等宽分为4

camp.Age_group1.head()

 

posted @ 2017-10-20 11:01  风起了,风停了  阅读(60721)  评论(4编辑  收藏  举报