三個(gè)Python常用的數(shù)據(jù)清洗處理方式總結(jié)
關(guān)于python數(shù)據(jù)處理過(guò)程中三個(gè)主要的數(shù)據(jù)清洗說(shuō)明,分別是缺失值/空格/重復(fù)值的數(shù)據(jù)清洗。
這里還是使用pandas來(lái)獲取excel或者csv的數(shù)據(jù)源來(lái)進(jìn)行數(shù)據(jù)處理。若是沒(méi)有pandas的非標(biāo)準(zhǔn)庫(kù)需要使用pip的方式安裝一下。
pip?install?pandas
準(zhǔn)備一下需要處理的臟數(shù)據(jù),這里選用的是excel數(shù)據(jù),也可以選擇其他的格式數(shù)據(jù),下面是源數(shù)據(jù)截圖。
使用pandas的read_excel()函數(shù)讀取出我們需要處理的data.xlsx文件。
#?Importing?the?pandas?library?and?giving?it?an?alias?of?pd. import?pandas?as?pd #?Reading?the?excel?file?and?storing?it?in?a?variable?called?`result_` result_?=?pd.read_excel('D:/test/data.xlsx') #?Printing?the?dataframe. print(result_)
注意,若是新的python環(huán)境直接安裝pandas模塊后執(zhí)行上面的讀取excel數(shù)據(jù)代碼可能會(huì)出現(xiàn)沒(méi)有openpyxl模塊的情況。
這時(shí)候,我們使用pip的方式再次安裝一下openpyxl即可。
pip?install?openpyxl
完成后再次執(zhí)行讀取excel數(shù)據(jù)的代碼塊會(huì)成功的返回結(jié)果。
#???????????姓名????年齡????班級(jí)???成績(jī)?表現(xiàn) #?0???Python?集中營(yíng)??10??1210???99??A #?1???Python?集中營(yíng)??11??1211??100??A #?2???Python?集中營(yíng)??12??1212??101??A #?3???Python?集中營(yíng)??13??1213??102??A #?4???Python?集中營(yíng)??14??1214??103??A #?5???Python?集中營(yíng)??15??1215??104??A #?6???Python?集中營(yíng)??16??1216??105??A #?7???Python?集中營(yíng)??17??1217??106??A #?8???Python?集中營(yíng)??18??1218??107??A #?9???Python?集中營(yíng)??19??1219??108??A #?10??Python?集中營(yíng)??20??1220??109??A #?11??Python?集中營(yíng)??21??1221??110??A #?12??Python?集中營(yíng)??22??1222??111??A #?13??Python?集中營(yíng)??23??1223??112??A #?14??Python?集中營(yíng)??24??1224??113??A #?15??Python?集中營(yíng)??25??1225??114??A #?16??Python?集中營(yíng)??26??1226??115??A #?17??Python?集中營(yíng)??27??1227??116??A #?18??Python?集中營(yíng)??28??1228??117??A # #?Process?finished?with?exit?code?0
準(zhǔn)備好數(shù)據(jù)源之后,我們使用三個(gè)方式來(lái)完成對(duì)源數(shù)據(jù)的數(shù)據(jù)清洗。
1. strip函數(shù)清除空格
首先,將所有的列名稱(chēng)提取出來(lái),使用DataFrame對(duì)象的columns函數(shù)進(jìn)行提取。
#?Extracting?the?column?names?from?the?dataframe?and?storing?it?in?a?variable?called?`columns_`. columns_?=?result_.columns.values #?Printing?the?column?names?of?the?dataframe. print(columns_) #?['??姓名??'?'年齡'?'班級(jí)'?'成績(jī)'?'表現(xiàn)']
從列名稱(chēng)的打印結(jié)果發(fā)現(xiàn)'姓名'這一列是存在空格的,我們直接查找列名稱(chēng)是找不到的,因?yàn)樾枰獙?duì)列名稱(chēng)的空格進(jìn)行數(shù)據(jù)清洗。
為了減少代碼塊的使用,我們這里直接使用列表推導(dǎo)式的方式對(duì)列名稱(chēng)的空格進(jìn)行清洗。
#?A?list?comprehension?that?is?iterating?over?the?`columns_`?list?and?stripping?the?whitespaces?from?each?element?of?the #?list. result_.columns?=?[column_name.strip()?for?column_name?in?columns_] #?Printing?the?column?names?of?the?dataframe. print(result_.columns.values) #?['姓名'?'年齡'?'班級(jí)'?'成績(jī)'?'表現(xiàn)']
經(jīng)過(guò)數(shù)據(jù)清洗后,發(fā)現(xiàn)所有的列名稱(chēng)空格情況已經(jīng)被全部清洗了。若是存在某個(gè)列中的值空格需要清洗也可以采用strip函數(shù)進(jìn)行清洗。
2. duplicated函數(shù)清除重復(fù)數(shù)據(jù)
關(guān)于重復(fù)數(shù)據(jù)的判斷有兩種情況,一種是兩行完全相同的數(shù)據(jù)即為重復(fù)數(shù)據(jù)。另外一種則是部分相同指的是某個(gè)列的數(shù)據(jù)是相同的需要清洗。
#?The?`duplicated()`?function?is?returning?a?boolean?series?that?is?True?if?the?row?is?a?duplicate?and?False?if?the?row?is #?not?a?duplicate. repeat_num?=?result_.duplicated().sum() #?Printing?the?number?of?duplicate?rows?in?the?dataframe. print(repeat_num) #?1
通過(guò)上面的duplicated().sum()函數(shù)得到的是兩個(gè)完全相同的數(shù)據(jù)行是多少。
接著則可以對(duì)源數(shù)據(jù)進(jìn)行實(shí)際意義上的刪除,使用DataFrame對(duì)象的drop_duplicates函數(shù)進(jìn)行刪除。
#?The?`drop_duplicates()`?function?is?dropping?the?duplicate?rows?from?the?dataframe?and?the?`inplace=True`?is #?modifying?the?dataframe?in?place. result_.drop_duplicates(inplace=True) #?Printing?the?dataframe. print(result_) #????????????姓名??年齡????班級(jí)???成績(jī)?表現(xiàn) #?0???Python?集中營(yíng)??10??1210???99??A #?1???Python?集中營(yíng)??11??1211??100??A #?2???Python?集中營(yíng)??12??1212??101??A #?3???Python?集中營(yíng)??13??1213??102??A #?4???Python?集中營(yíng)??14??1214??103??A #?5???Python?集中營(yíng)??15??1215??104??A #?6???Python?集中營(yíng)??16??1216??105??A #?7???Python?集中營(yíng)??17??1217??106??A #?8???Python?集中營(yíng)??18??1218??107??A #?9???Python?集中營(yíng)??19??1219??108??A #?10??Python?集中營(yíng)??20??1220??109??A #?11??Python?集中營(yíng)??21??1221??110??A #?12??Python?集中營(yíng)??22??1222??111??A #?13??Python?集中營(yíng)??23??1223??112??A #?14??Python?集中營(yíng)??24??1224??113??A #?15??Python?集中營(yíng)??25??1225??114??A #?16??Python?集中營(yíng)??26??1226??115??A #?17??Python?集中營(yíng)??27??1227??116??A
因?yàn)樽詈笠恍泻偷谝恍械臄?shù)據(jù)是完全相同的,因此最后一行的數(shù)據(jù)已經(jīng)被清洗掉了。
一般在數(shù)據(jù)清洗刪除重復(fù)值之后需要重置索引,避免索引產(chǎn)生不連續(xù)性。
#?The?`range(result_.shape[0])`?is?creating?a?list?of?numbers?from?0?to?the?number?of?rows?in?the?dataframe. result_.index?=?range(result_.shape[0]) #?The?`print(result_.index)`?is?printing?the?index?of?the?dataframe. print(result_.index) #?RangeIndex(start=0,?stop=18,?step=1)
3. 數(shù)據(jù)缺失值補(bǔ)全
一般查看DataFrame數(shù)據(jù)對(duì)象的缺失值就是通過(guò)使用isnull函數(shù)來(lái)提取所有數(shù)據(jù)缺失的部分。
#?The?`isnull()`?function?is?returning?a?boolean?series?that?is?True?if?the?value?is?missing?and?False?if?the?value #?is?not?missing. sul_?=?result_.isnull() #?The?`print(sul_)`?is?printing?the?boolean?series?that?is?True?if?the?value?is?missing?and?False?if?the?value?is?not #?missing. print(sul_) #????????姓名?????年齡?????班級(jí)?????成績(jī)?????表現(xiàn) #?0???False??False??False??False??False #?1???False??False??False??False??False #?2???False??False??False??False??False #?3???False??False??False??False??False #?4???False??False??False??False??False #?5???False??False??False??False??False #?6???False??False??False??False??False #?7???False??False??False??False??False #?8???False??False??False??False??False #?9???False??False??False??False??False #?10??False??False??False??False??False #?11??False??False??False??False??False #?12??False??False??False??False??False #?13??False??False??False??False??False #?14??False??False??False??False??False #?15??False??False??False??False??False #?16??False??False??False??False??False #?17??False??False??False??False??False
返回的每一個(gè)單元格數(shù)據(jù)結(jié)果為False則代表這個(gè)單元格的數(shù)據(jù)是沒(méi)有缺失的,或者也可以使用notnull來(lái)反向查看。
使用isnull函數(shù)不想顯示很多的列表數(shù)據(jù)時(shí),可以使用sum函數(shù)進(jìn)行統(tǒng)計(jì)。
#?The?`isnull_sum?=?result_.isnull().sum()`?is?returning?a?series?that?is?the?sum?of?the?boolean?series?that?is?True?if #?the?value?is?missing?and?False?if?the?value?is?not?missing. isnull_sum?=?result_.isnull().sum() #?The?`isnull_sum?=?result_.isnull().sum()`?is?returning?a?series?that?is?the?sum?of?the?boolean?series?that?is?True?if #?the?value?is?missing?and?False?if?the?value?is?not?missing. print(isnull_sum) #?姓名????0 #?年齡????0 #?班級(jí)????0 #?成績(jī)????0 #?表現(xiàn)????0 #?dtype:?int64
通過(guò)isnull函數(shù)處理后使用sum函數(shù)進(jìn)行統(tǒng)計(jì),統(tǒng)計(jì)后會(huì)返回每一列的數(shù)據(jù)單元格為空的個(gè)數(shù)。
接下來(lái)就是數(shù)據(jù)值的填補(bǔ)過(guò)程,通??梢院Y選每一列中的空值填補(bǔ)固定的數(shù)據(jù)。
#?The?`result_.loc[result_.姓名.isnull(),?'姓名']`?is?returning?a?series?that?is?the?values?of?the?column?`姓名` #?where?the?values?are?missing.?The?`'Python?集中營(yíng)'`?is?the?value?that?is?being?assigned?to?the?series. result_.loc[result_.姓名.isnull(),?'姓名']?=?'Python?集中營(yíng)' #?Printing?the?dataframe. print(result_) #?????????????姓名??年齡????班級(jí)???成績(jī)?表現(xiàn) #?0???Python?集中營(yíng)??10??1210???99??A #?1???Python?集中營(yíng)??11??1211??100??A #?2???Python?集中營(yíng)??12??1212??101??A #?3???Python?集中營(yíng)??13??1213??102??A #?4???Python?集中營(yíng)??14??1214??103??A #?5???Python?集中營(yíng)??15??1215??104??A #?6???Python?集中營(yíng)??16??1216??105??A #?7???Python?集中營(yíng)??17??1217??106??A #?8???Python?集中營(yíng)??18??1218??107??A #?9???Python?集中營(yíng)??19??1219??108??A #?10??Python?集中營(yíng)??20??1220??109??A #?11??Python?集中營(yíng)??21??1221??110??A #?12??Python?集中營(yíng)??22??1222??111??A #?13??Python?集中營(yíng)??23??1223??112??A #?14??Python?集中營(yíng)??24??1224??113??A #?15??Python?集中營(yíng)??25??1225??114??A #?16??Python?集中營(yíng)??26??1226??115??A #?17??Python?集中營(yíng)??27??1227??116??A
4. 數(shù)據(jù)保存
數(shù)據(jù)清洗完成之后,可以使用DataFrame對(duì)象提供的to_csv/to_excel等函數(shù)進(jìn)行特定格式的數(shù)據(jù)保存。
result_.to_excel('data.xlsx')
最后,整個(gè)數(shù)據(jù)清洗的過(guò)程就完成了!
到此這篇關(guān)于三個(gè)Python常用的數(shù)據(jù)清洗處理方式總結(jié)的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)清洗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Python進(jìn)行數(shù)據(jù)清洗與存儲(chǔ)的基本方法
- 如何使用Python數(shù)據(jù)清洗庫(kù)
- 使用python數(shù)據(jù)清洗代碼實(shí)例
- 用Python進(jìn)行數(shù)據(jù)清洗以及值處理
- Python常用的數(shù)據(jù)清洗方法詳解
- 一文帶你深入了解Python中的數(shù)據(jù)清洗
- Python數(shù)據(jù)清洗&預(yù)處理入門(mén)教程
- python?文件讀寫(xiě)和數(shù)據(jù)清洗
- Python實(shí)現(xiàn)數(shù)據(jù)清洗的示例詳解
- python數(shù)據(jù)清洗中的時(shí)間格式化實(shí)現(xiàn)
- Python實(shí)現(xiàn)數(shù)據(jù)清洗的18種方法
相關(guān)文章
關(guān)于python中密碼加鹽的學(xué)習(xí)體會(huì)小結(jié)
這篇文章主要介紹了關(guān)于python中密碼加鹽的學(xué)習(xí)體會(huì)小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python實(shí)現(xiàn)單項(xiàng)鏈表的最全教程
單向鏈表也叫單鏈表,是鏈表中最簡(jiǎn)單的一種形式,它的每個(gè)節(jié)點(diǎn)包含兩個(gè)域,一個(gè)信息域(元素域)和一個(gè)鏈接域,這個(gè)鏈接指向鏈表中的下一個(gè)節(jié)點(diǎn),而最后一個(gè)節(jié)點(diǎn)的鏈接域則指向一個(gè)空值,這篇文章主要介紹了Python實(shí)現(xiàn)單項(xiàng)鏈表,需要的朋友可以參考下2023-01-01Python 語(yǔ)言實(shí)現(xiàn)六大查找算法
本文給大家分享Python 語(yǔ)言實(shí)現(xiàn)六大查找算法,針對(duì)每種算法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06Python StrEnum基本概念和使用場(chǎng)景分析
StrEnum是Python枚舉家族的一個(gè)強(qiáng)大補(bǔ)充,特別適合處理字符串常量,它結(jié)合了枚舉的類(lèi)型安全性和字符串的靈活性,使得在許多場(chǎng)景下的編程變得更加簡(jiǎn)潔和安全,本文將介紹StrEnum的基本概念和使用場(chǎng)景,并通過(guò)示例代碼來(lái)展示它的實(shí)際應(yīng)用,感興趣的朋友跟隨小編一起看看吧2024-07-07