导入包:
from sklearn.impute import SampleImpute
先将一列数据初始化为一个二维的:
data['Age'] = data.loc[:, 'Age'].values.reshape(-1, 1)
开始填补缺失值:
imp_mean = SimpleImpute() # 默认用0填补
imp_median = SimpleImpute(strategy='median') #中位数填补
imp_0 = SimpleImpute(strategy='constant', fill_value=0) # 用0填补
然后通过fit_transform()将这些值填补进去:
imp_mean = imp_mean.fit_transform(Age)
imp_median = imp_median.fit_transform(Age)
...