詳解利用Pandas求解兩個(gè)DataFrame的差集,交集,并集
大家好,我是Peter~
本文講解的是如何利用Pandas函數(shù)求解兩個(gè)DataFrame的差集、交集、并集。
模擬數(shù)據(jù)
模擬一份簡(jiǎn)單的數(shù)據(jù):
In [1]:
import?pandas?as?pd
In [2]:
df1?=?pd.DataFrame({"col1":[1,2,3,4,5],
????????????????????"col2":[6,7,8,9,10]
???????????????????})
df2?=?pd.DataFrame({"col1":[1,3,7],
????????????????????"col2":[6,8,10]
???????????????????})
In [3]:
df1
Out[3]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
In [4]:
df2
Out[4]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 3 | 8 |
| 2 | 7 | 10 |
兩個(gè)DataFrame的相同部分:

差集
方法1:concat + drop_duplicates
In [5]:
df3?=?pd.concat([df1,df2]) df3
Out[5]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 0 | 1 | 6 |
| 1 | 3 | 8 |
| 2 | 7 | 10 |
In [6]:
#?結(jié)果1 df3.drop_duplicates(["col1","col2"],keep=False)
Out[6]:
| col1 | col2 | |
|---|---|---|
| 1 | 2 | 7 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 2 | 7 | 10 |
方法2:append + drop_duplicates
In [7]:
df4?=?df1.append(df2) df4
Out[7]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 0 | 1 | 6 |
| 1 | 3 | 8 |
| 2 | 7 | 10 |
In [8]:
#?結(jié)果2 df4.drop_duplicates(["col1","col2"],keep=False)
Out[8]:
| col1 | col2 | |
|---|---|---|
| 1 | 2 | 7 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 2 | 7 | 10 |
交集
方法1:merge
In [9]:
#?結(jié)果 #?等效:df5 = pd.merge(df1, df2, how="inner") df5?=?pd.merge(df1,df2) df5
Out[9]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 3 | 8 |
方法2:concat + duplicated + loc
In [10]:
df6?=?pd.concat([df1,df2]) df6
Out[10]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 0 | 1 | 6 |
| 1 | 3 | 8 |
| 2 | 7 | 10 |
In [11]:
s?=?df6.duplicated(subset=['col1','col2'],?keep='first') s
Out[11]:
0 False
1 False
2 False
3 False
4 False
0 True
1 True
2 False
dtype: bool
In [12]:
#?結(jié)果 df8?=?df6.loc[s?==?True] df8
Out[12]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 3 | 8 |
方法3:concat + groupby + query
In [13]:
#?df6?=?pd.concat([df1,df2]) df6
Out[13]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 0 | 1 | 6 |
| 1 | 3 | 8 |
| 2 | 7 | 10 |
In [14]:
df9?=?df6.groupby(["col1",?"col2"]).size().reset_index() df9.columns?=?["col1",?"col2",?"count"] df9
Out[14]:
| col1 | col2 | count | |
|---|---|---|---|
| 0 | 1 | 6 | 2 |
| 1 | 2 | 7 | 1 |
| 2 | 3 | 8 | 2 |
| 3 | 4 | 9 | 1 |
| 4 | 5 | 10 | 1 |
| 5 | 7 | 10 | 1 |
In [15]:
df10?=?df9.query("count?>?1")[["col1",?"col2"]]
df10
Out[15]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 2 | 3 | 8 |
并集
方法1:concat + drop_duplicates
In [16]:
df11?=?pd.concat([df1,df2]) df11
Out[16]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 0 | 1 | 6 |
| 1 | 3 | 8 |
| 2 | 7 | 10 |
In [17]:
#?結(jié)果 #?df12?=?df11.drop_duplicates(subset=["col1","col2"],keep="last") df12?=?df11.drop_duplicates(subset=["col1","col2"],keep="first") df12
Out[17]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 2 | 7 | 10 |
方法2:append + drop_duplicates
In [18]:
df13?=?df1.append(df2) #?df13.drop_duplicates(subset=["col1","col2"],keep="last") df13.drop_duplicates(subset=["col1","col2"],keep="first")
Out[18]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 2 | 7 | 10 |
方法3:merge
In [19]:
pd.merge(df1,df2,how="outer")
Out[19]:
| col1 | col2 | |
|---|---|---|
| 0 | 1 | 6 |
| 1 | 2 | 7 |
| 2 | 3 | 8 |
| 3 | 4 | 9 |
| 4 | 5 | 10 |
| 5 | 7 | 10 |
以上就是詳解利用Pandas求解兩個(gè)DataFrame的差集,交集,并集的詳細(xì)內(nèi)容,更多關(guān)于Pandas DataFrame差集 交集 并集的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python opencv對(duì)圖像進(jìn)行旋轉(zhuǎn)且不裁剪圖片的實(shí)現(xiàn)方法
今天小編就為大家分享一篇python opencv對(duì)圖像進(jìn)行旋轉(zhuǎn)且不裁剪圖片的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
web.py在SAE中的Session問(wèn)題解決方法(使用mysql存儲(chǔ))
這篇文章主要介紹了web.py在SAE中的Session問(wèn)題解決方法(使用mysql存儲(chǔ)),本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06
Matplotlib中%matplotlib inline如何使用
這篇文章主要介紹了Matplotlib中%matplotlib inline如何使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
一文帶你探索Python中15個(gè)常見(jiàn)的魔術(shù)方法
在Python中,特殊方法(也稱為魔術(shù)方法)是由Python解釋器自動(dòng)調(diào)用的,我們不需要手動(dòng)調(diào)用它們,本文小編為大家整理了15個(gè)常見(jiàn)特殊方法的實(shí)現(xiàn),希望對(duì)大家有所幫助2024-01-01
淺析python 動(dòng)態(tài)庫(kù)m.so.1.0錯(cuò)誤問(wèn)題
這篇文章主要介紹了python 動(dòng)態(tài)庫(kù)m.so.1.0錯(cuò)誤問(wèn)題,文中給大家提到了python中使用動(dòng)態(tài)庫(kù)的方法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python簡(jiǎn)單獲取網(wǎng)卡名稱及其IP地址的方法【基于psutil模塊】
這篇文章主要介紹了Python簡(jiǎn)單獲取網(wǎng)卡名稱及其IP地址的方法,結(jié)合實(shí)例形式分析了Python基于psutil模塊針對(duì)本機(jī)網(wǎng)卡硬件信息的讀取操作簡(jiǎn)單使用技巧,需要的朋友可以參考下2018-05-05

