其他分享
首页 > 其他分享> > 差集交集

差集交集

作者:互联网

两个df对象,要获取这两个df对象的交集和差集
取交集
inner_df = pd.merge(df1, df2, how='inner') ## 计算df1=df2的部份

left_df = pd.merge(df1, df2, how='left') ## df1部分

right_df = pd.merge(df1, df2, how='right') ## df2部分

outer_df = pd.merge(df1, df2, how='outer') ## 取合集:df1和df2所有数据的集合

取差集
利用了drop_duplicates
df1-df2
df = pd.concat([df1, df2, df2]).drop_duplicates(subset=['filed_name', 'filed_type'], keep=False)
df2-df1
df = pd.concat([df2, df1, df1]).drop_duplicates(subset=['filed_name', 'filed_type'], keep=False)

等同于
fileds_df = df1.append(df2).drop_duplicates(subset=['filed_name', 'filed_type'], keep=False)
fileds_df = df2.append(df1).drop_duplicates(subset=['filed_name', 'filed_type'], keep=False)

标签:交集,drop,df1,差集,df2,df,pd,filed
来源: https://www.cnblogs.com/0916m/p/14248169.html