Python數(shù)據(jù)預(yù)處理時缺失值的不同處理方式總結(jié)
在使用python做數(shù)據(jù)分析的時候,經(jīng)常需要先對數(shù)據(jù)做統(tǒng)一化的處理,缺失值的處理是經(jīng)常會使用到的。
一般情況下,缺失值的處理要么是刪除缺失數(shù)據(jù)所在的行,要么就是對缺失的單元格數(shù)據(jù)進行填充。
今天介紹的是使用差補法/均值/固定值等不同的方式完成數(shù)據(jù)填充從而保證數(shù)據(jù)的完整性!
這里采用的還是pandas模塊的DataFrame數(shù)據(jù)對象來做數(shù)據(jù)處理,因此,沒有pandas的話使用pip的方式安裝一下即可。
pip?install?pandas
下面是我們需要處理的源數(shù)據(jù),由于是本地測試數(shù)據(jù),數(shù)據(jù)量比較小。
使用pandas模塊的read_excel函數(shù)將源數(shù)據(jù)全部讀取出來返回DataFrame對象。
#?Importing?the?pandas?module?and?giving?it?the?alias?pd. import?pandas?as?pd #?Reading?the?excel?file?and?storing?it?in?a?dataframe. data_frame?=?pd.read_excel('D:/test-data-work/data.xlsx') #?Printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級?????成績???表現(xiàn) #?0???Python?集中營??10.0??1210.0???99.0????A #?1???Python?集中營??11.0??1211.0??100.0????A #?2???Python?集中營??12.0??1212.0??101.0????A #?3???Python?集中營??13.0??1213.0??102.0????A #?4???Python?集中營??14.0??1214.0??103.0??NaN #?5???Python?集中營??15.0??1215.0??104.0????A #?6???Python?集中營??16.0??1216.0??105.0????A #?7???Python?集中營??17.0?????NaN??106.0????A #?8???Python?集中營??18.0??1218.0????NaN????A #?9???Python?集中營??19.0??1219.0??108.0????A #?10??Python?集中營???NaN??1220.0??109.0??NaN #?11??Python?集中營???NaN?????NaN??110.0????A #?12??Python?集中營???NaN??1222.0????NaN????A #?13??Python?集中營??23.0??1223.0??112.0????A #?14??Python?集中營??24.0??1224.0??113.0????A #?15??Python?集中營??25.0?????NaN????NaN??NaN #?16??Python?集中營???NaN??1226.0??115.0????A #?17??Python?集中營??27.0??1227.0????NaN????A #?18??Python?集中營??10.0??1210.0???99.0??NaN
源數(shù)據(jù)已經(jīng)讀取完成了,接下來使用四種常見的缺失值的處理方式來進行批量的數(shù)據(jù)填充。
1. 固定值填充
固定值填充也是一種比較簡單并且常用的填充方式,只需要給某個列填充自己想要填充的值即可。
這里我們把'表現(xiàn)'這一個列的空值全部填充成'B',fillna函數(shù)就是填充空值的意思。
#?Replacing?all?the?NaN?values?in?the?column?'表現(xiàn)'?with?the?value?'B'. data_frame['表現(xiàn)']?=?data_frame['表現(xiàn)'].fillna('B') #?Printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級?????成績?表現(xiàn) #?0???Python?集中營??10.0??1210.0???99.0??A #?1???Python?集中營??11.0??1211.0??100.0??A #?2???Python?集中營??12.0??1212.0??101.0??A #?3???Python?集中營??13.0??1213.0??102.0??A #?4???Python?集中營??14.0??1214.0??103.0??B #?5???Python?集中營??15.0??1215.0??104.0??A #?6???Python?集中營??16.0??1216.0??105.0??A #?7???Python?集中營??17.0?????NaN??106.0??A #?8???Python?集中營??18.0??1218.0????NaN??A #?9???Python?集中營??19.0??1219.0??108.0??A #?10??Python?集中營???NaN??1220.0??109.0??B #?11??Python?集中營???NaN?????NaN??110.0??A #?12??Python?集中營???NaN??1222.0????NaN??A #?13??Python?集中營??23.0??1223.0??112.0??A #?14??Python?集中營??24.0??1224.0??113.0??A #?15??Python?集中營??25.0?????NaN????NaN??B #?16??Python?集中營???NaN??1226.0??115.0??A #?17??Python?集中營??27.0??1227.0????NaN??A #?18??Python?集中營??10.0??1210.0???99.0??B
2. 均值填充
均值填充就是將缺失值所在列的數(shù)據(jù)進行一次均值計算,計算出結(jié)果后再填充到缺失值所在的單元格上面。
使用均值填充的前提是這一列的數(shù)據(jù)可以進行均值計算,比如'成績'這一列都是數(shù)字可以使用mean函數(shù)做均值計算。
#?Replacing?all?the?NaN?values?in?the?column?'成績'?with?the?mean?of?the?column?'成績'. data_frame['成績']?=?data_frame['成績'].fillna(data_frame['成績'].mean()) #?It's?printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級??????????成績?表現(xiàn) #?0???Python?集中營??10.0??1210.0???99.000000??A #?1???Python?集中營??11.0??1211.0??100.000000??A #?2???Python?集中營??12.0??1212.0??101.000000??A #?3???Python?集中營??13.0??1213.0??102.000000??A #?4???Python?集中營??14.0??1214.0??103.000000??B #?5???Python?集中營??15.0??1215.0??104.000000??A #?6???Python?集中營??16.0??1216.0??105.000000??A #?7???Python?集中營??17.0?????NaN??106.000000??A #?8???Python?集中營??18.0??1218.0??105.733333??A #?9???Python?集中營??19.0??1219.0??108.000000??A #?10??Python?集中營???NaN??1220.0??109.000000??B #?11??Python?集中營???NaN?????NaN??110.000000??A #?12??Python?集中營???NaN??1222.0??105.733333??A #?13??Python?集中營??23.0??1223.0??112.000000??A #?14??Python?集中營??24.0??1224.0??113.000000??A #?15??Python?集中營??25.0?????NaN??105.733333??B #?16??Python?集中營???NaN??1226.0??115.000000??A #?17??Python?集中營??27.0??1227.0??105.733333??A #?18??Python?集中營??10.0??1210.0???99.000000??B
可以發(fā)現(xiàn)計算出的均值是105.733333,已經(jīng)都填充到'成績'這一列的缺失值上面了。
3. 中位數(shù)填充
中位數(shù)填充和均值填充差不多是一樣的,不同的是使用median函數(shù)來計算缺失值所在列的中位數(shù)。
#?Replacing?all?the?NaN?values?in?the?column?'年齡'?with?the?median?of?the?column?'年齡'. data_frame['年齡']?=?data_frame['年齡'].fillna(data_frame['年齡'].median()) #?It's?printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級??????????成績?表現(xiàn) #?0???Python?集中營??10.0??1210.0???99.000000??A #?1???Python?集中營??11.0??1211.0??100.000000??A #?2???Python?集中營??12.0??1212.0??101.000000??A #?3???Python?集中營??13.0??1213.0??102.000000??A #?4???Python?集中營??14.0??1214.0??103.000000??B #?5???Python?集中營??15.0??1215.0??104.000000??A #?6???Python?集中營??16.0??1216.0??105.000000??A #?7???Python?集中營??17.0?????NaN??106.000000??A #?8???Python?集中營??18.0??1218.0??105.733333??A #?9???Python?集中營??19.0??1219.0??108.000000??A #?10??Python?集中營??16.0??1220.0??109.000000??B #?11??Python?集中營??16.0?????NaN??110.000000??A #?12??Python?集中營??16.0??1222.0??105.733333??A #?13??Python?集中營??23.0??1223.0??112.000000??A #?14??Python?集中營??24.0??1224.0??113.000000??A #?15??Python?集中營??25.0?????NaN??105.733333??B #?16??Python?集中營??16.0??1226.0??115.000000??A #?17??Python?集中營??27.0??1227.0??105.733333??A #?18??Python?集中營??10.0??1210.0???99.000000??B
4. 插補法填充
差補法填充可以根據(jù)該列的上一個數(shù)據(jù)和下一個數(shù)據(jù)得到該單元格需要插入的數(shù)據(jù)是多少。
比如:上一個班級是1220,下一個班級是1222,那么該單元格需要插入的數(shù)據(jù)應(yīng)該是1221。
#?Replacing?all?the?NaN?values?in?the?column?'班級'?with?the?interpolated?values?of?the?column?'班級'. data_frame['班級']?=?data_frame['班級'].interpolate() #?It's?printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級??????????成績?表現(xiàn) #?0???Python?集中營??10.0??1210.0???99.000000??A #?1???Python?集中營??11.0??1211.0??100.000000??A #?2???Python?集中營??12.0??1212.0??101.000000??A #?3???Python?集中營??13.0??1213.0??102.000000??A #?4???Python?集中營??14.0??1214.0??103.000000??B #?5???Python?集中營??15.0??1215.0??104.000000??A #?6???Python?集中營??16.0??1216.0??105.000000??A #?7???Python?集中營??17.0??1217.0??106.000000??A #?8???Python?集中營??18.0??1218.0??105.733333??A #?9???Python?集中營??19.0??1219.0??108.000000??A #?10??Python?集中營??16.0??1220.0??109.000000??B #?11??Python?集中營??16.0??1221.0??110.000000??A #?12??Python?集中營??16.0??1222.0??105.733333??A #?13??Python?集中營??23.0??1223.0??112.000000??A #?14??Python?集中營??24.0??1224.0??113.000000??A #?15??Python?集中營??25.0??1225.0??105.733333??B #?16??Python?集中營??16.0??1226.0??115.000000??A #?17??Python?集中營??27.0??1227.0??105.733333??A #?18??Python?集中營??10.0??1210.0???99.000000??B
到此這篇關(guān)于Python數(shù)據(jù)預(yù)處理時缺失值的不同處理方式總結(jié)的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)預(yù)處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python將請求的requests headers參數(shù)格式化方法
今天小編就為大家分享一篇使用python將請求的requests headers參數(shù)格式化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù)
這篇文章主要介紹了PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù),在本次的分類任務(wù)當(dāng)中,我們使用的數(shù)據(jù)集是 Mnist 數(shù)據(jù)集,這個數(shù)據(jù)集大家都比較熟悉,需要的朋友可以參考下2023-03-03Python中ROS和OpenCV結(jié)合處理圖像問題
ROS通過一個叫CvBridge的功能包,將獲取的圖像數(shù)據(jù)轉(zhuǎn)換成OpenCV的格式,OpenCV處理之后,傳回給ROS進行圖像顯示(應(yīng)用),這篇文章主要介紹了Python中ROS和OpenCV結(jié)合處理圖像問題,需要的朋友可以參考下2022-06-06pandas?實現(xiàn)?in?和?not?in?的用法及使用心得
pandas按條件篩選數(shù)據(jù)時,除了使用query()方法,還可以使用isin和對isin取反進行條件篩選,今天通過本文給大家介紹pandas?實現(xiàn)?in?和?not?in?的用法及使用心得,感興趣的朋友跟隨小編一起看看吧2023-01-01python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中
這篇文章主要介紹了python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04