手把手教你搞定4類數(shù)據(jù)清洗操作
導(dǎo)讀:本文介紹數(shù)據(jù)清洗的相關(guān)內(nèi)容,主要涉及缺失值清洗、格式內(nèi)容清洗、邏輯錯誤清洗和維度相關(guān)性檢查四個方面。

# 檢查數(shù)據(jù)缺失情況
def check_missing_data(df):
return df.isnull().sum().sort_values(ascending = False)
check_missing_data(rawdata)
Income 1
Age 1
Online Shopper 0
Region 0
dtype: int64

test1 = rawdata.copy()# 將更改前的數(shù)據(jù)進行備份
test1 = test1.head(3)# 提取前三行進行測試
test1 = test1.dropna()# 去除數(shù)據(jù)中有缺失值的行
print(test1)
test1
name toy born
0 Andy NaN NaN
1 Cindy Gun 1998-12-25
2 Wendy Gum NaN
test1 = test1.dropna(axis=0)# 去除數(shù)據(jù)中有缺失值的行
name toy born
1 Cindy Gun 1998-12-25
test1 = test1.dropna(axis='columns')# 去除數(shù)據(jù)中有缺失值的列
name
0 Andy
1 Cindy
2 Wendy
test1 = test1.dropna(how='all')# 去除數(shù)據(jù)完全缺失的行
test1 = test1.dropna(thresh=2)# 保留行中至少有兩個值的行
test1 = test1.dropna(how='any')# 去除數(shù)據(jù)中含有缺失值的行
test1 = test1.dropna(how='any',subset=['toy'])# 去除toy列中含有缺失值的行
test1.dropna(inplace=True)# 在這個變量名中直接保存結(jié)果
test1 = test1.fillna(test1.mean())# 用均值填充缺失值
test1 = test1.fillna(test1.median())# 用中位數(shù)填充缺失值
test1 = test1.fillna(test1.mode())# 用眾數(shù)填充缺失值

dataset.isna().sum() # 統(tǒng)計各列缺失值情況

# 設(shè)定填充方式為平均值填充
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
# 選取目標(biāo)列
imputer = imputer.fit(rawdata.iloc[:,1:3])
# 對計算結(jié)果進行填充
rawdata.iloc[:,1:3] = imputer.transform(rawdata.iloc[:,1:3])
# 調(diào)整數(shù)據(jù)
rawdata.iloc[:,1:3] = rawdata.iloc[:,1:3].round(0).astype(int)
-
身份證號必須是數(shù)字+字母。 -
中國人姓名只能為漢字(李A(yù)、張C這種情況是少數(shù))。 -
出現(xiàn)在頭、尾、中間的空格。
-
每日食品中卡路里攝入量跟體重很有可能有較大的相關(guān)性; -
子女和父母血型之間具有高關(guān)聯(lián)性; -
學(xué)習(xí)的時間長度和考試成績通常也有高關(guān)聯(lián)性。
rawdata.corr() # 相關(guān)性矩陣

rawdata.cov() # 協(xié)方差矩陣

劃重點??
干貨直達??
評論
圖片
表情
