生成缺失值
import pandas as pd
import numpy as np
#from sklearn.preprocessing import Imputer
from sklearn.impute import SimpleImputer
df = pd.DataFrame(np.random.randn(6,4) , columns = ['col1','col2','col3','col4'])
df.iloc[1:2,1] = np.nan
df.iloc[4,3] = np.nan
#loc [ raw , col ]:第一个参数raw表示行选,行选只能是整数;后面的参数表示列选,列选只能是索引名称,比如姓名、年龄之类的
#iloc [ raw , col ]:第一个参数raw表示行选,第二个参数表示列选,都必须是整数
print(df)
判断缺失值
nan_all = df.isnull()
print(nan_all)
nan_col1 = df.isnull().any()
nan_col2 = df.isnull().all()
print(nan_col1)
print(nan_col2)
丢弃异常值
df2 = df.dropna()
print(df2)
sklearn处理
#nan_model = Imputer(missing_values = 'NaN' , strategy = 'mean' , axis = 0)
nan_model = SimpleImputer(missing_values =np.nan , strategy = 'mean' )
nan_result = nan_model.fit_transform(df)
print(nan_result)
pandas处理
nan_result_pd1 = df.fillna(method = 'backfill') #后面值代替
nan_result_pd2 = df.fillna(method = 'bfill',limit = 1) #限制每列只有一个
nan_result_pd3 = df.fillna(method = 'pad') #前面值代替
nan_result_pd4 = df.fillna(0) #用0代替
nan_result_pd5 = df.fillna({'col2':1.1,'col4':1.2})
nan_result_pd6 = df.fillna(df.mean()['col2':'col4'])#各自列的均值
print(nan_result_pd1)
print(nan_result_pd2)
print(nan_result_pd3)
print(nan_result_pd4)
print(nan_result_pd5)
print(nan_result_pd6)