当前位置:网站首页>Get the intersection union difference set of two dataframes
Get the intersection union difference set of two dataframes
2022-06-26 14:51:00 【Sheep sheep pig】
Example

intersection

df_merge = pd.merge(df1,df2,on=['id','name','number'],how='inner')

Combine

# Two ways
df_union = pd.merge(df1,df2,on=['id','name','number'],how='outer')
df_union2 = df1.append(df2).drop_duplicates(subset=df1.columns,keep='first',ignore_index=True)

Difference set

# Two ways
# df1-df2
df_diff1 = df1.append(df1.append(df2)).drop_duplicates(subset=df1.columns,keep=False,ignore_index=True)
df_diff1 = pd.concat([df1,df1,df2]).drop_duplicates(keep=False)

# Two ways
# df2 - df1
df_diff2 = df2.append(df2.append(df1)).drop_duplicates(subset=df2.columns,keep=False,ignore_index=True)
df_diff2 = pd.concat([df1,df2,df2]).drop_duplicates(keep=False)

Symmetric difference set

df_diff = df1.append(df2).drop_duplicates(subset=df1.columns,keep=False,ignore_index=True)

Reference source
pandas.DataFrame.drop_duplicates Official documents
pandas.merge Official documents
边栏推荐
- Attention meets geometry: geometry guided spatiotemporal attention consistency self supervised monocular depth estimation
- Caelus - full scene offline mixed Department solution
- How to personalize VIM editor format (DIY)
- Matplotlib common operations
- The annual salary of 500000 is one line, and the annual salary of 1million is another line
- MHA高可用配合及故障切换
- JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了
- transformers DataCollatorWithPadding类
- GDAL grid data types and their type codes
- 备战数学建模32-相关性分析2
猜你喜欢

在云服务器中云磁盘如何挂载

Chinese output of PostGIS console is garbled

Use abp Zero builds a third-party login module (I): Principles

人的顶级能量从哪里获取?

Solution to the upper limit of TeamViewer display devices

How to mount cloud disks in ECS

Mark: unity3d cannot select resources in the inspector, that is, project locking

【soloπ】adb连接单个多个手机

Electron

Unity uses skybox panoramic shader to make panorama preview. There is a gap. Solution
随机推荐
Correlation of XOR / and
Complimentary Book Cognitive Control: how does our brain accomplish tasks?
Transformers datacollatorwithpadding class
(improved) bubble sorting and (improved) cocktail sorting
710. 黑名单中的随机数
Mark一下 Unity3d在Inspector中选中不了资源即Project锁定问题
赠书 | 《认知控制》:我们的大脑如何完成任务?
feil_uVission4左侧工目录消失
Go变量的声明与赋值
Solution to the upper limit of TeamViewer display devices
BM3D in popular language
备战数学建模32-相关性分析2
Summary of decimal point of amount and price at work and pit
MHA高可用配合及故障切换
datasets Dataset类(2)
VIM auto fill auto indent explanation
Login authentication service
Use of subqueries
[cloud native] codeless IVX editor programmable by "everyone"
Recent important news