根据列减去 2 个 pyspark 数据帧

Subtract 2 pyspark dataframes based on column

我有 2 个 pyspark 数据帧,

i
+---+-----+
| ID|COL_A|
+---+-----+
|  1|  123|
|  2|  456|
|  3|  111|
|  4|  678|
+---+-----+
j
+----+-----+
|ID_B|COL_B|
+----+-----+
|   2|  456|
|   3|  111|
|   4|  876|
+----+-----+

我正在尝试根据特定列的值从 j 中减去 i,即 iCOL_A 中存在的值不应出现在 jCOL_B 中。

预期输出应该是,

diff
+---+-----+
| ID|COL_A|
+---+-----+
|  1|  123|
|  4|  678|
+---+-----+

这是我的代码,

common = i.join(j.withColumnRenamed('COL_B', 'COL_A'), ['COL_A'], 'leftsemi')
diff = i.subtract(common)
diff.show()

但是输出出错了,

diff
+---+-----+
| ID|COL_A|
+---+-----+
|  2|  456|
|  1|  123|
|  4|  678|
|  3|  111|
+---+-----+

我是不是做错了什么?提前致谢。

尝试:

left_join = i.join(j, j.COL_B == i.COL_A,how='left')
left_join.filter(left_join.COL_A.isNull()).show()

如果您将列名作为参数,您可以这样做:

left_join = i.join(j, j[colb] == i[cola],how='left')
left_join.filter(left_join[cola].isNull()).show()