詳解利用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中實(shí)現(xiàn)單例模式的幾種常見(jiàn)方式
這篇文章主要為大家介紹了Python中實(shí)現(xiàn)單例模式的幾種常見(jiàn)方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python二進(jìn)制轉(zhuǎn)化為十進(jìn)制數(shù)學(xué)算法詳解
這篇文章主要介紹了Python二進(jìn)制轉(zhuǎn)化為十進(jìn)制數(shù)學(xué)算法,同時(shí)在這里也給大家分享一個(gè)好用的內(nèi)置函數(shù)map(),需要的朋友可以參考下2023-01-01python將字母轉(zhuǎn)化為數(shù)字實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于python如何將字母轉(zhuǎn)化為數(shù)字的相關(guān)實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-10-10關(guān)于Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了Python 實(shí)現(xiàn)tuple和list的轉(zhuǎn)換,文中介紹了list(列表)和tuple(元組)共同點(diǎn)和區(qū)別,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Python使用pandas將表格數(shù)據(jù)進(jìn)行處理
這篇文章主要介紹了Python使用pandas將表格數(shù)據(jù)進(jìn)行處理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08Python中強(qiáng)大的函數(shù)map?filter?reduce使用詳解
Python是一門(mén)功能豐富的編程語(yǔ)言,提供了許多內(nèi)置函數(shù),以簡(jiǎn)化各種編程任務(wù),在Python中,map(),filter()和reduce()是一組非常有用的函數(shù),它們?cè)试S對(duì)可迭代對(duì)象進(jìn)行操作,從而實(shí)現(xiàn)數(shù)據(jù)轉(zhuǎn)換、篩選和累積等操作,本文將詳細(xì)介紹這三個(gè)函數(shù),包括它們的基本用法和示例代碼2023-11-11Python應(yīng)用開(kāi)發(fā)之實(shí)現(xiàn)串口通信
在嵌入式開(kāi)發(fā)中我們經(jīng)常會(huì)用到串口,串口通信簡(jiǎn)單,使用起來(lái)方便,且適用場(chǎng)景多。本文為大家準(zhǔn)備了Python實(shí)現(xiàn)串口通信的示例代碼,需要的可以參考一下2022-11-11python實(shí)現(xiàn)猜拳游戲項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)猜拳游戲項(xiàng)目,以excel形式保存信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11