检测与处理缺失值
- 准备数据
准备数据detail.csv,将数据文件detail.csv放到Linux本地的/course/DataAnalyze/data目录,并读取数据
In[1]: | import os import pandas as pd os.chdir('/course/DataAnalyze/data') detail = pd.read_csv('./detail.csv',index_col=0,encoding = 'gbk', engine = 'python') |
- 检测与处理缺失值
pandas提供了识别缺失值的方法isnull以及识别非缺失值的方法notnull,这两种方法在使用时返回的都是布尔值True和False。结合sum方法和isnull、notnull方法,可以检测数据中缺失值的分布以及数据中一共含有多少缺失值,具体用法如代码 410所示。
In[9]: | print('detail每个特征缺失的数目为:\n',detail.isnull().sum()) print('detail每个特征非缺失的数目为:\n',detail.notnull().sum()) | |
Out[9]: | detail每个特征缺失的数目为: order_id 0 dishes_id 0 logicprn_name 1546 dishes_name 0 itemis_add 0 counts 0 amounts 0 place_order_time 0 picture_file 0 emp_id 0 dtype: int64 | detail每个特征非缺失的数目为: order_id 1546 dishes_id 1546 logicprn_name 0 dishes_name 1546 itemis_add 1546 counts 1546 amounts 1546 place_order_time 1546 picture_file 1546 emp_id 1546 dtype: int64 |
- 删除法处理
- pandas中提供了简便的删除缺失值的方法dropna,对菜品订单详情表利用dropna方法进行缺失值处理,如代码 411所示。
In[10]: | print('去除缺失的列前detail的形状为:', detail.shape) print('去除缺失的列后detail的形状为:',detail.dropna(axis = 1,how ='any').shape) |
Out[10]: | 去除缺失的列前detail的形状为: (1546, 11) 去除缺失的列后detail的形状为: (1546, 7) |
- 当how参数取值为“any”时,删除了1个特征,说明这个特征存在缺失值。若how参数不取“any”这个默认取值而取“all”,则表示这一整个特征全部为缺失值时才会执行删除操作。
- 替换法处理
pandas库中提供了缺失值替换的方法名为fillna,将缺失值用常量-99进行填补,如代码 412所示,填补之后logicprn_name特征中的缺失值已经不复存在。
In[11]: | detail = detail.fillna(-99) print('detail每个特征缺失的数目为:\n',detail.isnull().sum()) |
Out[11]: | detail每个特征缺失的数目为: order_id 0 dishes_id 0 logicprn_name 0 dishes_name 0 itemis_add 0 counts 0 amounts 0 place_order_time 0 picture_file 0 emp_id 0 dtype: int64 |
- 插值法处理
- pandas提供了对应的名为interpolate的插值方法,能够进行上述部分插值操作,但是SciPy的interpolate模块更加全面,其具体用法如代码 413所示。
代码 413 SciPy interpolate模块插值
In[12]: | ## 线性插值 Import numpy as np from scipy.interpolate import interp1d x=np.array([1,2,3,4,5,8,9,10]) ##创建自变量x y1=np.array([2,8,18,32,50,128,162,200]) ##创建因变量y1 y2=np.array([3,5,7,9,11,17,19,21]) ##创建因变量y2 LinearInsValue1 = interp1d(x,y1,kind='linear') ##线性插值拟合x,y1 LinearInsValue2 = interp1d(x,y2,kind='linear') ##线性插值拟合x,y2 print('当x为6、7时,使用线性插值y1为:',LinearInsValue1([6,7])) print('当x为6、7时,使用线性插值y2为:',LinearInsValue2([6,7])) |
Out[12]: | 当x为6、7时,使用线性插值y1为:[ 76. 102.] 当x为6、7时,使用线性插值y2为:[ 13. 15.] |
In[13]: | ## 拉格朗日插值 from scipy.interpolate import lagrange LargeInsValue1 = lagrange(x,y1) ##拉格朗日插值拟合x,y1 LargeInsValue2 = lagrange(x,y2) ##拉格朗日插值拟合x,y2 print('当x为6,7时,使用拉格朗日插值y1为:',LargeInsValue1([6,7])) print('当x为6,7时,使用拉格朗日插值y2为:',LargeInsValue2([6,7])) |
Out[13]: | 当x为6,7时,使用拉格朗日插值y1为:[ 72. 98.] 当x为6,7时,使用拉格朗日插值y2为:[ 13. 15.] |
In[14]: | ##样条插值 from scipy.interpolate import spline SplineInsValue1 = spline(x,y1,xnew=np.array([6,7])) ##样条插值拟合x,y1 SplineInsValue2 = spline(x,y2,xnew=np.array([6,7])) ##样条插值拟合x,y2 print('当x为6,7时,使用样条插值y1为:',SplineInsValue1) print('当x为6,7时,使用样条插值y2为:',SplineInsValue2) |
Out[14]: | 当x为6,7时,使用样条插值y1为:[ 72. 98.] 当x为6,7时,使用样条插值y2为:[ 13. 15.] |
- 统计出经过去重操作后的各个特征缺失值,然后根据缺失情况选择缺失值处理方法,如代码 414所示。
In[18]: | ##统计各个特征的缺失率 naRate = (detail.isnull().sum()/detail.shape[0]*100).astype('str')+'%' print('detail每个特征缺失的率为:\n',naRate) ##删除全部均为缺失的列 detail.dropna(axis = 1,how = 'all',inplace = True) print('经过缺失值处理后订单详情表各特征缺失值的数目为:\n',detail.isnull().sum()) | |
Out[18]: | detail每个特征缺失的率为: order_id 0.0% dishes_id 0.0% logicprn_name 0.0% dishes_name 0.0% itemis_add 0.0% counts 0.0% amounts 0.0% place_order_time 0.0% picture_file 0.0% emp_id 0.0% dtype: object | 经过缺失值处理后订单详情表各特征缺失值的数目为: order_id 0 dishes_id 0 dishes_name 0 itemis_add 0 counts 0 amounts 0 place_order_time 0 picture_file 0 emp_id 0 dtype: int64 |