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

#?檢查數(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ù)進(jìn)行備份
test1?=?test1.head(3)#?提取前三行進(jìn)行測試
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)#?保留行中至少有兩個(gè)值的行
test1?=?test1.dropna(how='any')#?去除數(shù)據(jù)中含有缺失值的行
test1?=?test1.dropna(how='any',subset=['toy'])#?去除toy列中含有缺失值的行
test1.dropna(inplace=True)#?在這個(gè)變量名中直接保存結(jié)果test1?=?test1.fillna(test1.mean())#?用均值填充缺失值
test1?=?test1.fillna(test1.median())#?用中位數(shù)填充缺失值
test1?=?test1.fillna(test1.mode())#?用眾數(shù)填充缺失值

dataset.isna().sum()??????????#?統(tǒng)計(jì)各列缺失值情況
#?設(shè)定填充方式為平均值填充
imputer?=?SimpleImputer(missing_values=np.nan,?strategy='mean')??
#?選取目標(biāo)列
imputer?=?imputer.fit(rawdata.iloc[:,1:3])?
#?對計(jì)算結(jié)果進(jìn)行填充
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í)的時(shí)間長度和考試成績通常也有高關(guān)聯(lián)性。
rawdata.corr()?????#?相關(guān)性矩陣?
rawdata.cov()??????#?協(xié)方差矩陣
(歡迎大家加入數(shù)據(jù)工匠知識星球獲取更多資訊。)

掃描二維碼關(guān)注我們

我們的使命:發(fā)展數(shù)據(jù)治理行業(yè)、普及數(shù)據(jù)治理知識、改變企業(yè)數(shù)據(jù)管理現(xiàn)狀、提高企業(yè)數(shù)據(jù)質(zhì)量、推動企業(yè)走進(jìn)大數(shù)據(jù)時(shí)代。
我們的愿景:打造數(shù)據(jù)治理專家、數(shù)據(jù)治理平臺、數(shù)據(jù)治理生態(tài)圈。
我們的價(jià)值觀:凝聚行業(yè)力量、打造數(shù)據(jù)治理全鏈條平臺、改變數(shù)據(jù)治理生態(tài)圈。

了解更多精彩內(nèi)容
長按,識別二維碼,關(guān)注我們吧!
數(shù)據(jù)工匠俱樂部
微信號:zgsjgjjlb
評論
圖片
表情
