class sklearn.preprocessing.Imputer(missing_values=’NaN’, strategy=’mean’, axis=0, verbose=0, copy=True)
参数:
- missing_values: integer or “NaN”, optional (default=”NaN”)
- strategy : string, optional (default=”mean”)
- The imputation strategy.
- If “mean”, then replace missing values using the mean along the axis. 使用平均值代替
- If “median”, then replace missing values using the median along the axis.使用中值代替
- If “most_frequent”, then replace missing using the most frequent value along the axis.使用众数代替,也就是出现次数最多的数
- The imputation strategy.
- axis: 默认为 axis=0
- axis = 0, 按列处理
- aixs =1 , 按行处理
说实话,我还是没太弄明白aixs的具体含义,总感觉在不同的函数中有不同的含义。。还是使用前查找一下官方文档吧,毕竟大多数时候处理的都是2维数组,文档中的参数很容易理解。
注意:
- Imputer 只接受DataFrame类型
- Dataframe 中必须全部为数值属性
所以在处理的时候注意,要进行适当处理。
- 数值属性的列较少,可以将数值属性的列取出来 单独取出来
import pandas as pd
import numpy as np
df=pd.DataFrame([["XXL", 8, "black", "class 1", 22],
["L", np.nan, "gray"