我有两个具有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减查询时返回null。我使用以下查询,但未获得预期的结果。有什么办法可以达到我想要的结果吗?
df3 = df1.merge(df2.drop_duplicates(),how='right', indicator=True)
print(df3)
例如,如果我将df1视为table1并将df2视为table2,并且如果我在SQL Server中运行以下查询,则将返回null(空表)。
SELECT * FROM table1
EXCEPT
SELECT * FROM table2
是的,您可以像这样使用指标:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句