網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
詳解Python如何利用Pandas與NumPy進(jìn)行數(shù)據(jù)清洗_python
作者:Mr數(shù)據(jù)楊 ? 更新時(shí)間: 2022-06-13 編程語(yǔ)言許多數(shù)據(jù)科學(xué)家認(rèn)為獲取和清理數(shù)據(jù)的初始步驟占工作的 80%,花費(fèi)大量時(shí)間來(lái)清理數(shù)據(jù)集并將它們歸結(jié)為可以使用的形式。
因此如果你是剛剛踏入這個(gè)領(lǐng)域或計(jì)劃踏入這個(gè)領(lǐng)域,重要的是能夠處理雜亂的數(shù)據(jù),無(wú)論數(shù)據(jù)是否包含缺失值、不一致的格式、格式錯(cuò)誤的記錄還是無(wú)意義的異常值。
將利用 Python 的 Pandas和 NumPy 庫(kù)來(lái)清理數(shù)據(jù)。
準(zhǔn)備工作
導(dǎo)入模塊后就開(kāi)始正式的數(shù)據(jù)預(yù)處理吧。
import pandas as pd
import numpy as np
DataFrame 列的刪除
通常會(huì)發(fā)現(xiàn)并非數(shù)據(jù)集中的所有數(shù)據(jù)類(lèi)別都有用。例如可能有一個(gè)包含學(xué)生信息(姓名、年級(jí)、標(biāo)準(zhǔn)、父母姓名和地址)的數(shù)據(jù)集,但希望專(zhuān)注于分析學(xué)生成績(jī)。在這種情況下地址或父母的姓名并不重要。保留這些不需要的數(shù)據(jù)將占用不必要的空間。
BL-Flickr-Images-Book.csv 數(shù)據(jù)操作。
df = pd.read_csv('數(shù)據(jù)科學(xué)必備Pandas、NumPy進(jìn)行數(shù)據(jù)清洗/BL-Flickr-Images-Book.csv')
df.head()
可以看到這些列是對(duì) Edition Statement, Corporate Author, Corporate Contributors, Former owner, Engraver, Issuance type and Shelfmarks 沒(méi)有任何信息幫助的,因此可以進(jìn)行批量刪除處理。
to_drop_column = [ 'Edition Statement',
?? ??? ? ? ? ? ? ? 'Corporate Author',
?? ??? ? ? ? ? ? ? 'Corporate Contributors',
?? ??? ? ? ? ? ? ? 'Former owner',
?? ??? ? ? ? ? ? ? 'Engraver',
?? ??? ? ? ? ? ? ? 'Contributors',
?? ??? ? ? ? ? ? ? 'Issuance type',
?? ??? ? ? ? ? ? ? 'Shelfmarks']
df.drop(to_drop_column , inplace=True, axis=1)
df.head()
DataFrame 索引更改
Pandas 索引擴(kuò)展了 NumPy 數(shù)組的功能,以允許更通用的切片和標(biāo)記。 在許多情況下,使用數(shù)據(jù)的唯一值標(biāo)識(shí)字段作為其索引是有幫助的。
獲取唯一標(biāo)識(shí)符。
df['Identifier'].is_unique
True
Identifier列替換索引列。
df = df.set_index('Identifier')
df.head()
206 是索引的第一個(gè)標(biāo)簽,可以使用 df.iloc[0] 基于位置的索引訪(fǎng)問(wèn)。
DataFrame 數(shù)據(jù)字段整理
清理特定列并將它們轉(zhuǎn)換為統(tǒng)一格式,以更好地理解數(shù)據(jù)集并強(qiáng)制保持一致性。
處理 Date of Publication 出版日期 列,發(fā)現(xiàn)該數(shù)據(jù)列格式并不統(tǒng)一。
df.loc[1905:, 'Date of Publication'].head(10)
Identifier
1905 1888
1929 1839, 38-54
2836 1897
2854 1865
2956 1860-63
2957 1873
3017 1866
3131 1899
4598 1814
4884 1820
Name: Date of Publication, dtype: object
我們可以使用正則表達(dá)式的方式直接提取連續(xù)的4個(gè)數(shù)字即可。
extr = df['Date of Publication'].str.extract(r'^(\d{4})', expand=False)
extr.head()
Identifier
206 ? ?1879
216 ? ?1868
218 ? ?1869
472 ? ?1851
480 ? ?1857
Name: Date of Publication, dtype: object
最后獲取數(shù)字字段列。
df['Date of Publication'] = pd.to_numeric(extr)
str 方法與 NumPy 結(jié)合清理列
df[‘Date of Publication’].str 。 此屬性是一種在 Pandas 中訪(fǎng)問(wèn)快速字符串操作的方法,這些操作在很大程度上模仿了對(duì)原生 Python 字符串或編譯的正則表達(dá)式的操作,例如 .split()、.replace() 和 .capitalize()。
要清理 Place of Publication 字段,我們可以將 Pandas 的 str 方法與 NumPy 的 np.where 函數(shù)結(jié)合起來(lái),該函數(shù)基本上是 Excel 的 IF() 宏的矢量化形式。
np.where(condition, then, else)
在這里 condition 要么是一個(gè)類(lèi)似數(shù)組的對(duì)象,要么是一個(gè)布爾掩碼。 then 是如果條件評(píng)估為 True 時(shí)使用的值,否則是要使用的值。
本質(zhì)上 .where() 獲取用于條件的對(duì)象中的每個(gè)元素,檢查該特定元素在條件上下文中的計(jì)算結(jié)果是否為 True,并返回一個(gè)包含 then 或 else 的 ndarray,具體取決于哪個(gè)適用。可以嵌套在復(fù)合 if-then 語(yǔ)句中,允許根據(jù)多個(gè)條件計(jì)算值.
處理 Place of Publication 出版地 數(shù)據(jù)。
df['Place of Publication'].head(10)
Identifier
206 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?London
216 ? ? ? ? ? ? ? ?London; Virtue & Yorston
218 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?London
472 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?London
480 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?London
481 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?London
519 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?London
667 ? ? pp. 40. G. Bryan & Co: Oxford, 1898
874 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? London]
1143 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? London
Name: Place of Publication, dtype: object
使用包含的方式提取需要的數(shù)據(jù)信息。
pub = df['Place of Publication']
london = pub.str.contains('London')
london[:5]
Identifier
206 ? ?True
216 ? ?True
218 ? ?True
472 ? ?True
480 ? ?True
Name: Place of Publication, dtype: bool
也可以使用 np.where 處理。
df['Place of Publication'] = np.where(london, 'London',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pub.str.replace('-', ' ')))
Identifier
206 ? ? ? ? ? ? ? ? ? ? London
216 ? ? ? ? ? ? ? ? ? ? London
218 ? ? ? ? ? ? ? ? ? ? London
472 ? ? ? ? ? ? ? ? ? ? London
480 ? ? ? ? ? ? ? ? ? ? London
? ? ? ? ? ? ? ? ? ... ? ? ? ??
4158088 ? ? ? ? ? ? ? ? London
4158128 ? ? ? ? ? ? ? ? ?Derby
4159563 ? ? ? ? ? ? ? ? London
4159587 ? ?Newcastle upon Tyne
4160339 ? ? ? ? ? ? ? ? London
Name: Place of Publication, Length: 8287, dtype: object
apply 函數(shù)清理整個(gè)數(shù)據(jù)集
在某些情況下,將自定義函數(shù)應(yīng)用于 DataFrame 的每個(gè)單元格或元素。 Pandas.apply() 方法類(lèi)似于內(nèi)置的 map() 函數(shù),只是將函數(shù)應(yīng)用于 DataFrame 中的所有元素。
例如將數(shù)據(jù)的發(fā)布日期進(jìn)行處理成 xxxx 年的格式,就可以使用apply。
def clean_date(text):
try:
return str(int(text)) + "年"
except:
return text
df["new_date"] = df["Date of Publication"].apply(clean_date)
df["new_date"]
Identifier
206 1879年
216 1868年
218 1869年
472 1851年
480 1857年
...
4158088 1838年
4158128 1831年
4159563 NaN
4159587 1834年
4160339 1834年
Name: new_date, Length: 8287, dtype: object
DataFrame 跳過(guò)行
olympics_df = pd.read_csv('數(shù)據(jù)科學(xué)必備Pandas、NumPy進(jìn)行數(shù)據(jù)清洗/olympics.csv')
olympics_df.head()
可以在讀取數(shù)據(jù)時(shí)候添加參數(shù)跳過(guò)某些不要的行,比如索引 0 行。
olympics_df = pd.read_csv('數(shù)據(jù)科學(xué)必備Pandas、NumPy進(jìn)行數(shù)據(jù)清洗/olympics.csv',header=1)
olympics_df.head()
DataFrame 重命名列
new_names = {'Unnamed: 0': 'Country',
'? Summer': 'Summer Olympics',
'01 !': 'Gold',
'02 !': 'Silver',
'03 !': 'Bronze',
'? Winter': 'Winter Olympics',
'01 !.1': 'Gold.1',
'02 !.1': 'Silver.1',
'03 !.1': 'Bronze.1',
'? Games': '# Games',
'01 !.2': 'Gold.2',
'02 !.2': 'Silver.2',
'03 !.2': 'Bronze.2'}
olympics_df.rename(columns=new_names, inplace=True)
olympics_df.head()
原文鏈接:https://blog.csdn.net/qq_20288327/article/details/124120569
相關(guān)推薦
- 2022-09-15 關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比_C 語(yǔ)言
- 2023-02-15 react源碼合成事件深入解析_React
- 2022-05-27 Flutter組件狀態(tài)管理的3種方法_Android
- 2022-08-04 docker安裝elastic?search的詳細(xì)過(guò)程_docker
- 2022-06-22 C++深入探究類(lèi)與對(duì)象之友元與運(yùn)算符重載_C 語(yǔ)言
- 2022-03-24 c++中的bind使用方法_C 語(yǔ)言
- 2023-03-28 Pytorch實(shí)現(xiàn)將label變成one?hot編碼的兩種方式_python
- 2022-08-28 強(qiáng)制緩存和協(xié)商緩存的區(qū)別是什么?對(duì)稱(chēng)、非對(duì)稱(chēng)加密的區(qū)別是什么?
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支