【Python】圖解Pandas重復(fù)值處理
公眾號(hào):尤而小屋
作者:Peter
編輯:Peter
今天帶來的文章是關(guān)于Pandas中重復(fù)值處理。Pandas中處理重復(fù)值主要使用的是兩個(gè)函數(shù):
duplicated():判斷是否有重復(fù)值 drop_duplicates() :刪除重復(fù)值

一、模擬數(shù)據(jù)
在本文中模擬了兩份不同的數(shù)據(jù):
1、一份訂單數(shù)據(jù),后面會(huì)使用
import pandas as pd
import numpy as np
# 導(dǎo)入一份模擬數(shù)據(jù):待用
df1 = pd.read_excel("訂單重復(fù)值.xlsx")
df1

2、模擬的另一份數(shù)據(jù):
df2 = pd.DataFrame(np.ones([10,2]), # 生成6*2的全部為1的數(shù)據(jù)
columns=["col1","col2"]
)
df2

再增加了兩個(gè)字段:都是從列表中隨機(jī)有抽樣放回的選取
# 增加兩列
list1 = ["a","b"]
list2 = [2,3]
# 在列表中隨機(jī)選擇10個(gè)元素,有放回抽樣
df2["col3"] = np.random.choice(list1,10)
df2["col4"] = np.random.choice(list2,10)
df2

二、判斷重復(fù)值-duplicated()
函數(shù)的功能是檢查數(shù)據(jù)中是否有重復(fù)值,用于標(biāo)記 Series 中的值、DataFrame 中的記錄行是否重復(fù),重復(fù)為 True,不重復(fù)為 False。
每行數(shù)據(jù)都是和它前面的記錄相比較。
2.1語法
針對(duì)DataFrame類型數(shù)據(jù):
pandas.DataFrame.duplicated(subset=None,keep='first')
或者針對(duì)Series的數(shù)據(jù):
pandas.Series.duplicated(keep='first')
keep參數(shù)的3種取值解釋:
first:將重復(fù)項(xiàng)標(biāo)記True為第一次出現(xiàn)的除外 last:將重復(fù)項(xiàng)標(biāo)記True為最后一次除外 False:將所有重復(fù)項(xiàng)標(biāo)記為True
2.2基本使用
通過這個(gè)函數(shù)能夠判斷哪些數(shù)據(jù)是重復(fù)的:重復(fù)標(biāo)記為True,否則為False

2.3參數(shù)subset
df2.duplicated(subset=["col3"]) # 單獨(dú)看col3列是否重復(fù)
# 結(jié)果
0 False
1 True
2 False
3 True
4 True
5 True
6 True
7 True
8 True
9 True
dtype: bool
df2.duplicated(subset=["col1"]) # 單獨(dú)看col1:全部是1,后面全部是重復(fù)的
0 False
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
dtype: bool
上面的兩個(gè)例子都是看單個(gè)字段是否重復(fù),下面的例子是通過查看多個(gè)屬性:
df2.duplicated(subset=["col3","col4"]) # 同時(shí)看col3和col4
0 False
1 True
2 False
3 True
4 True
5 True
6 True
7 False
8 False
9 True
dtype: bool
2.4參數(shù)keep
df2.duplicated(subset=["col3"],keep="last")
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 False # 第一次出現(xiàn)
8 True
9 False # 第一次出現(xiàn)
dtype: bool

上面的keep參數(shù)使用的是last,相當(dāng)于是最后的一條數(shù)據(jù)是初始值,前面的值和它進(jìn)行比較,是否有重復(fù)值 下面的案例中keep使用的first(默認(rèn)),相當(dāng)于是將第一次出現(xiàn)的數(shù)據(jù)看做是初始值,后面的數(shù)據(jù)和它相比;如果重復(fù)標(biāo)記為True
df2.duplicated(subset=["col3"],keep="first") # 默認(rèn)是first
0 False # 第一次出現(xiàn)
1 True
2 False # 第一次出現(xiàn)
3 True
4 True
5 True
6 True
7 True
8 True
9 True
dtype: bool
df2.duplicated(subset=["col3"],keep=False) # 將所有的重復(fù)值標(biāo)記為True
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
dtype: bool
三、drop_duplicates()
該函數(shù)的作用是刪除數(shù)據(jù)中的重復(fù)值
3.1語法形式
subset:表示按照指定的一個(gè)或者多個(gè)列屬性來刪除重復(fù)值,可選性;默認(rèn)是全部列屬性 keep:表示刪除重復(fù)值后保留的數(shù)據(jù),默認(rèn)是保留第一條數(shù)據(jù) inplace:表示刪除重復(fù)是生成副本,還是直接在原數(shù)據(jù)上進(jìn)行修改。這個(gè)參數(shù)的功能在pandas的功能都是如此 ingoore_index:生成數(shù)據(jù)的索引是元數(shù)據(jù)的,還是從0,1,2...到n-1的自然數(shù)排列
下面是來自官網(wǎng)的參數(shù)解釋:

3.2全部使用默認(rèn)參數(shù)

上面的結(jié)果有兩個(gè)特點(diǎn):
索引還是原數(shù)據(jù)的索引 保留的數(shù)據(jù)是每條值的第一條(如果存在重復(fù)值) 判斷是否重復(fù),使用的是全部列屬性 上面的數(shù)據(jù)就是下面判斷是否重復(fù)的為False的數(shù)據(jù)(對(duì)比序號(hào))
3.3參數(shù)subset
subset是可以指定我們想通過哪些屬性來進(jìn)行刪除:
1、通過單個(gè)屬性字段來刪除

2、通過多個(gè)字段屬性來刪除

3.4參數(shù)keep
keep參數(shù)保留我們想要的數(shù)據(jù):第一條還是最后一條
1、keep="first"

2、keep="last"
通過duplicated()查看數(shù)據(jù)是否重復(fù),可以看多索引為7和9的數(shù)據(jù)為False,因?yàn)樗鼈兪亲詈笠淮纬霈F(xiàn)

3.5參數(shù)ignore_index
該參數(shù)表示的是生成數(shù)據(jù)的索引是原數(shù)據(jù)的索引還是直接重新排名

3.6參數(shù)inplace
如果是使用默認(rèn)值False:

如果inplace使用True,不會(huì)生成數(shù)據(jù),因?yàn)槭窃谠瓟?shù)據(jù)的基礎(chǔ)上修改的,導(dǎo)致原數(shù)據(jù)直接變化了:我們直接看df2

四、實(shí)戰(zhàn)案例
在文章的最開始,我們已經(jīng)導(dǎo)入了數(shù)據(jù),幾點(diǎn)需求說明:
每個(gè)訂單可能存在多個(gè)狀態(tài),也可能只存在一個(gè) 我們想要找出最終的訂單狀態(tài)為“通過”的訂單的所有數(shù)據(jù)

比如訂單S1,存在3條狀態(tài),有兩條是通過的,但是我們只想取出最近的一條通過的數(shù)據(jù):2021-01-06

解決步驟1:先找出通過的全部訂單,發(fā)現(xiàn)只有S7沒有通過

通過下面的代碼也能夠找出最終是通過的訂單:
order_pass = df1.query("狀態(tài) == '通過'")["訂單號(hào)"].unique()
order_pass

解決步驟2:篩選出最終狀態(tài)為通過的訂單信息,下面提供了兩種方式


解決步驟3:對(duì)df3進(jìn)行去重即可
df3.drop_duplicates(
subset="訂單號(hào)", # 根據(jù)訂單號(hào)去重
keep="last", # 保留最后一條
inplace=True, # 原地修改
ignore_index=True # 索引重排
)
df3 # 結(jié)果中沒有S7

五、Pandas連載文章
Pandas的文章已經(jīng)形成連載,歡迎關(guān)注閱讀:

往期精彩回顧 本站qq群851320808,加入微信群請掃碼:
