Python數(shù)據(jù)預(yù)處理時(shí)缺失值的不同處理方式總結(jié)
在使用python做數(shù)據(jù)分析的時(shí)候,經(jīng)常需要先對(duì)數(shù)據(jù)做統(tǒng)一化的處理,缺失值的處理是經(jīng)常會(huì)使用到的。
一般情況下,缺失值的處理要么是刪除缺失數(shù)據(jù)所在的行,要么就是對(duì)缺失的單元格數(shù)據(jù)進(jìn)行填充。
今天介紹的是使用差補(bǔ)法/均值/固定值等不同的方式完成數(shù)據(jù)填充從而保證數(shù)據(jù)的完整性!
這里采用的還是pandas模塊的DataFrame數(shù)據(jù)對(duì)象來(lái)做數(shù)據(jù)處理,因此,沒(méi)有pandas的話使用pip的方式安裝一下即可。
pip?install?pandas
下面是我們需要處理的源數(shù)據(jù),由于是本地測(cè)試數(shù)據(jù),數(shù)據(jù)量比較小。

使用pandas模塊的read_excel函數(shù)將源數(shù)據(jù)全部讀取出來(lái)返回DataFrame對(duì)象。
#?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)
#???????????姓名??????年齡??????班級(jí)?????成績(jī)???表現(xiàn)
#?0???Python?集中營(yíng)??10.0??1210.0???99.0????A
#?1???Python?集中營(yíng)??11.0??1211.0??100.0????A
#?2???Python?集中營(yíng)??12.0??1212.0??101.0????A
#?3???Python?集中營(yíng)??13.0??1213.0??102.0????A
#?4???Python?集中營(yíng)??14.0??1214.0??103.0??NaN
#?5???Python?集中營(yíng)??15.0??1215.0??104.0????A
#?6???Python?集中營(yíng)??16.0??1216.0??105.0????A
#?7???Python?集中營(yíng)??17.0?????NaN??106.0????A
#?8???Python?集中營(yíng)??18.0??1218.0????NaN????A
#?9???Python?集中營(yíng)??19.0??1219.0??108.0????A
#?10??Python?集中營(yíng)???NaN??1220.0??109.0??NaN
#?11??Python?集中營(yíng)???NaN?????NaN??110.0????A
#?12??Python?集中營(yíng)???NaN??1222.0????NaN????A
#?13??Python?集中營(yíng)??23.0??1223.0??112.0????A
#?14??Python?集中營(yíng)??24.0??1224.0??113.0????A
#?15??Python?集中營(yíng)??25.0?????NaN????NaN??NaN
#?16??Python?集中營(yíng)???NaN??1226.0??115.0????A
#?17??Python?集中營(yíng)??27.0??1227.0????NaN????A
#?18??Python?集中營(yíng)??10.0??1210.0???99.0??NaN
源數(shù)據(jù)已經(jīng)讀取完成了,接下來(lái)使用四種常見(jiàn)的缺失值的處理方式來(lái)進(jìn)行批量的數(shù)據(jù)填充。
1. 固定值填充
固定值填充也是一種比較簡(jiǎn)單并且常用的填充方式,只需要給某個(gè)列填充自己想要填充的值即可。
這里我們把'表現(xiàn)'這一個(gè)列的空值全部填充成'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)
#???????????姓名??????年齡??????班級(jí)?????成績(jī)?表現(xiàn)
#?0???Python?集中營(yíng)??10.0??1210.0???99.0??A
#?1???Python?集中營(yíng)??11.0??1211.0??100.0??A
#?2???Python?集中營(yíng)??12.0??1212.0??101.0??A
#?3???Python?集中營(yíng)??13.0??1213.0??102.0??A
#?4???Python?集中營(yíng)??14.0??1214.0??103.0??B
#?5???Python?集中營(yíng)??15.0??1215.0??104.0??A
#?6???Python?集中營(yíng)??16.0??1216.0??105.0??A
#?7???Python?集中營(yíng)??17.0?????NaN??106.0??A
#?8???Python?集中營(yíng)??18.0??1218.0????NaN??A
#?9???Python?集中營(yíng)??19.0??1219.0??108.0??A
#?10??Python?集中營(yíng)???NaN??1220.0??109.0??B
#?11??Python?集中營(yíng)???NaN?????NaN??110.0??A
#?12??Python?集中營(yíng)???NaN??1222.0????NaN??A
#?13??Python?集中營(yíng)??23.0??1223.0??112.0??A
#?14??Python?集中營(yíng)??24.0??1224.0??113.0??A
#?15??Python?集中營(yíng)??25.0?????NaN????NaN??B
#?16??Python?集中營(yíng)???NaN??1226.0??115.0??A
#?17??Python?集中營(yíng)??27.0??1227.0????NaN??A
#?18??Python?集中營(yíng)??10.0??1210.0???99.0??B2. 均值填充
均值填充就是將缺失值所在列的數(shù)據(jù)進(jìn)行一次均值計(jì)算,計(jì)算出結(jié)果后再填充到缺失值所在的單元格上面。
使用均值填充的前提是這一列的數(shù)據(jù)可以進(jìn)行均值計(jì)算,比如'成績(jī)'這一列都是數(shù)字可以使用mean函數(shù)做均值計(jì)算。
#?Replacing?all?the?NaN?values?in?the?column?'成績(jī)'?with?the?mean?of?the?column?'成績(jī)'. data_frame['成績(jī)']?=?data_frame['成績(jī)'].fillna(data_frame['成績(jī)'].mean()) #?It's?printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級(jí)??????????成績(jī)?表現(xiàn) #?0???Python?集中營(yíng)??10.0??1210.0???99.000000??A #?1???Python?集中營(yíng)??11.0??1211.0??100.000000??A #?2???Python?集中營(yíng)??12.0??1212.0??101.000000??A #?3???Python?集中營(yíng)??13.0??1213.0??102.000000??A #?4???Python?集中營(yíng)??14.0??1214.0??103.000000??B #?5???Python?集中營(yíng)??15.0??1215.0??104.000000??A #?6???Python?集中營(yíng)??16.0??1216.0??105.000000??A #?7???Python?集中營(yíng)??17.0?????NaN??106.000000??A #?8???Python?集中營(yíng)??18.0??1218.0??105.733333??A #?9???Python?集中營(yíng)??19.0??1219.0??108.000000??A #?10??Python?集中營(yíng)???NaN??1220.0??109.000000??B #?11??Python?集中營(yíng)???NaN?????NaN??110.000000??A #?12??Python?集中營(yíng)???NaN??1222.0??105.733333??A #?13??Python?集中營(yíng)??23.0??1223.0??112.000000??A #?14??Python?集中營(yíng)??24.0??1224.0??113.000000??A #?15??Python?集中營(yíng)??25.0?????NaN??105.733333??B #?16??Python?集中營(yíng)???NaN??1226.0??115.000000??A #?17??Python?集中營(yíng)??27.0??1227.0??105.733333??A #?18??Python?集中營(yíng)??10.0??1210.0???99.000000??B
可以發(fā)現(xiàn)計(jì)算出的均值是105.733333,已經(jīng)都填充到'成績(jī)'這一列的缺失值上面了。
3. 中位數(shù)填充
中位數(shù)填充和均值填充差不多是一樣的,不同的是使用median函數(shù)來(lái)計(jì)算缺失值所在列的中位數(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) #???????????姓名??????年齡??????班級(jí)??????????成績(jī)?表現(xiàn) #?0???Python?集中營(yíng)??10.0??1210.0???99.000000??A #?1???Python?集中營(yíng)??11.0??1211.0??100.000000??A #?2???Python?集中營(yíng)??12.0??1212.0??101.000000??A #?3???Python?集中營(yíng)??13.0??1213.0??102.000000??A #?4???Python?集中營(yíng)??14.0??1214.0??103.000000??B #?5???Python?集中營(yíng)??15.0??1215.0??104.000000??A #?6???Python?集中營(yíng)??16.0??1216.0??105.000000??A #?7???Python?集中營(yíng)??17.0?????NaN??106.000000??A #?8???Python?集中營(yíng)??18.0??1218.0??105.733333??A #?9???Python?集中營(yíng)??19.0??1219.0??108.000000??A #?10??Python?集中營(yíng)??16.0??1220.0??109.000000??B #?11??Python?集中營(yíng)??16.0?????NaN??110.000000??A #?12??Python?集中營(yíng)??16.0??1222.0??105.733333??A #?13??Python?集中營(yíng)??23.0??1223.0??112.000000??A #?14??Python?集中營(yíng)??24.0??1224.0??113.000000??A #?15??Python?集中營(yíng)??25.0?????NaN??105.733333??B #?16??Python?集中營(yíng)??16.0??1226.0??115.000000??A #?17??Python?集中營(yíng)??27.0??1227.0??105.733333??A #?18??Python?集中營(yíng)??10.0??1210.0???99.000000??B
4. 插補(bǔ)法填充
差補(bǔ)法填充可以根據(jù)該列的上一個(gè)數(shù)據(jù)和下一個(gè)數(shù)據(jù)得到該單元格需要插入的數(shù)據(jù)是多少。
比如:上一個(gè)班級(jí)是1220,下一個(gè)班級(jí)是1222,那么該單元格需要插入的數(shù)據(jù)應(yīng)該是1221。
#?Replacing?all?the?NaN?values?in?the?column?'班級(jí)'?with?the?interpolated?values?of?the?column?'班級(jí)'. data_frame['班級(jí)']?=?data_frame['班級(jí)'].interpolate() #?It's?printing?the?dataframe. print(data_frame) #???????????姓名??????年齡??????班級(jí)??????????成績(jī)?表現(xiàn) #?0???Python?集中營(yíng)??10.0??1210.0???99.000000??A #?1???Python?集中營(yíng)??11.0??1211.0??100.000000??A #?2???Python?集中營(yíng)??12.0??1212.0??101.000000??A #?3???Python?集中營(yíng)??13.0??1213.0??102.000000??A #?4???Python?集中營(yíng)??14.0??1214.0??103.000000??B #?5???Python?集中營(yíng)??15.0??1215.0??104.000000??A #?6???Python?集中營(yíng)??16.0??1216.0??105.000000??A #?7???Python?集中營(yíng)??17.0??1217.0??106.000000??A #?8???Python?集中營(yíng)??18.0??1218.0??105.733333??A #?9???Python?集中營(yíng)??19.0??1219.0??108.000000??A #?10??Python?集中營(yíng)??16.0??1220.0??109.000000??B #?11??Python?集中營(yíng)??16.0??1221.0??110.000000??A #?12??Python?集中營(yíng)??16.0??1222.0??105.733333??A #?13??Python?集中營(yíng)??23.0??1223.0??112.000000??A #?14??Python?集中營(yíng)??24.0??1224.0??113.000000??A #?15??Python?集中營(yíng)??25.0??1225.0??105.733333??B #?16??Python?集中營(yíng)??16.0??1226.0??115.000000??A #?17??Python?集中營(yíng)??27.0??1227.0??105.733333??A #?18??Python?集中營(yíng)??10.0??1210.0???99.000000??B
到此這篇關(guān)于Python數(shù)據(jù)預(yù)處理時(shí)缺失值的不同處理方式總結(jié)的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)預(yù)處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python進(jìn)程間通信multiprocess代碼實(shí)例
這篇文章主要介紹了Python進(jìn)程間通信multiprocess代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
使用python將請(qǐng)求的requests headers參數(shù)格式化方法
今天小編就為大家分享一篇使用python將請(qǐng)求的requests headers參數(shù)格式化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類(lèi)任務(wù)
這篇文章主要介紹了PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類(lèi)任務(wù),在本次的分類(lèi)任務(wù)當(dāng)中,我們使用的數(shù)據(jù)集是 Mnist 數(shù)據(jù)集,這個(gè)數(shù)據(jù)集大家都比較熟悉,需要的朋友可以參考下2023-03-03
Python中ROS和OpenCV結(jié)合處理圖像問(wèn)題
ROS通過(guò)一個(gè)叫CvBridge的功能包,將獲取的圖像數(shù)據(jù)轉(zhuǎn)換成OpenCV的格式,OpenCV處理之后,傳回給ROS進(jìn)行圖像顯示(應(yīng)用),這篇文章主要介紹了Python中ROS和OpenCV結(jié)合處理圖像問(wèn)題,需要的朋友可以參考下2022-06-06
pandas?實(shí)現(xiàn)?in?和?not?in?的用法及使用心得
pandas按條件篩選數(shù)據(jù)時(shí),除了使用query()方法,還可以使用isin和對(duì)isin取反進(jìn)行條件篩選,今天通過(guò)本文給大家介紹pandas?實(shí)現(xiàn)?in?和?not?in?的用法及使用心得,感興趣的朋友跟隨小編一起看看吧2023-01-01
python PIL模塊與隨機(jī)生成中文驗(yàn)證碼
今天我們要學(xué)習(xí)的內(nèi)容是如何利用Python生成一個(gè)隨機(jī)的中文驗(yàn)證碼,并將圖片保存為.jpeg格式,需要的朋友可以參考下2016-02-02
Python 啟動(dòng)時(shí)選擇32位 或64位版的操作
這篇文章主要介紹了Python 啟動(dòng)時(shí)選擇32位 或64位版的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Flask框架Jinjia模板常用語(yǔ)法總結(jié)
這篇文章主要介紹了Flask框架Jinjia模板常用語(yǔ)法,結(jié)合實(shí)例形式總結(jié)分析了Jinjia模板的變量、賦值、流程控制、函數(shù)、塊、宏等基本使用方法,需要的朋友可以參考下2018-07-07
python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中
這篇文章主要介紹了python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

