網(wǎng)站首頁 編程語言 正文
大家好,我是Peter~
本文講解的是如何利用Pandas函數(shù)求解兩個(gè)DataFrame的差集、交集、并集。
模擬數(shù)據(jù)
模擬一份簡單的數(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 |
原文鏈接:https://mp.weixin.qq.com/s/kmuVEdt13c8qRFA6w5lYFw
相關(guān)推薦
- 2022-07-29 Linux文件管理方法介紹_linux shell
- 2021-12-19 Linux下wget命令詳細(xì)介紹_Linux
- 2022-05-27 redis分布式Jedis類型轉(zhuǎn)換的異常深入研究_Redis
- 2022-06-07 python中字符串String及其常見操作指南(方法、函數(shù))_python
- 2023-03-27 react使用.env文件管理全局變量的方法_React
- 2023-01-02 Kotlin中空判斷與問號和感嘆號標(biāo)識符使用方法_Android
- 2022-03-12 一文搞懂MemoryCache?清除全部緩存的方法_實(shí)用技巧
- 2023-02-12 C++?STL之string的模擬實(shí)現(xiàn)實(shí)例代碼_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- 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)證過濾器
- Spring Security概述快速入門
- 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)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支