如何正确使用两个数据帧之间的SQL减号查询等价项
我有两个数据帧,每个数据帧有1000行。数据帧是相同的,但是逐行的数据帧并不相同。以下示例可以假定为数据帧的截断版本如何正确使用两个数据帧之间的SQL减号查询等价项,sql,python-3.x,pandas,dataframe,Sql,Python 3.x,Pandas,Dataframe,我有两个数据帧,每个数据帧有1000行。数据帧是相同的,但是逐行的数据帧并不相同。以下示例可以假定为数据帧的截断版本 df1: col1 col2 col3 1 2 3 2 3 4 5 6 6 8 9 9 df2: col1 col2 col3 5 6 6 8 9 9 1 2 3 2 3 4 数据帧没有索引,我希望在这些数据帧上实现sql减号查询时返回nul
df1:
col1 col2 col3
1 2 3
2 3 4
5 6 6
8 9 9
df2:
col1 col2 col3
5 6 6
8 9 9
1 2 3
2 3 4
数据帧没有索引,我希望在这些数据帧上实现sql减号查询时返回null。我使用了以下查询,但没有获得预期的结果。有什么方法可以达到我想要的结果吗
df3 = df1.merge(df2.drop_duplicates(),how='right', indicator=True)
print(df3)
例如,如果我认为DF1为<强>表1< /强>,DF2为<强>表2<强>,如果在SQL Server中运行以下查询,则返回空(空表)。
是的,您可以这样使用指示器:
df1.merge(df2, how='left', indicator='ind').query('ind=="left_only"')
其中,df1是:
col1 col2 col3
0 1.0 2.0 3.0
1 2.0 3.0 4.0
2 5.0 6.0 6.0
3 8.0 9.0 9.0
4 10.0 10.0 10.0
df2是:
col1 col2 col3
0 5 6 6
1 8 9 9
2 1 2 3
3 2 3 4
输出:
col1 col2 col3 ind
4 10.0 10.0 10.0 left_only
您可以尝试:
df1.loc[df1.isin(df2.any(1)]
或df1.merge(df2,indicator=True).查询(“\u merge!='both'))).drop(columns=“\u merge”)
或df1.merge(df2,indicator=True).iloc[lambda-df:df.iloc[:,-1].ne(“both”).array,:-1]
。如果我理解正确,您正在寻找类似于反连接的东西,其中df2都不在df1中。
col1 col2 col3 ind
4 10.0 10.0 10.0 left_only