leijing0607

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

 

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

 

分类:

技术点:

相关文章:

  • 2021-10-15
  • 2021-12-13
  • 2021-12-13
  • 2021-10-29
猜你喜欢
  • 2021-07-11
  • 2021-11-07
  • 2021-09-27
  • 2021-12-13
  • 2021-12-13
  • 2021-12-15
  • 2018-01-05
相关资源
相似解决方案