Pyspark数据框-如何过滤出与另一个数据框匹配的记录?

时间:2020-01-22 22:23:54

标签: dataframe apache-spark pyspark

我在Spark中有2个数据框。

DF1:

c1,c2
a,1
b,2
c,3

DF2:

c1,c2
d,4
e,5
a,6

我想从DF1中选择所有记录,除了DF2中与C1列匹配的记录(值'a'在第二个数据帧中,因此需要从第一个数据帧中过滤掉记录)。

结果应为:

DF3:

c1,c2
b,2
c,3

1 个答案:

答案 0 :(得分:1)

您可以将exceptAll用作

df3 = df1.select("C1").exceptAll(df2.select("C1"))

result = df1.join(df3, df1.C1 == df3.C1).drop(df3.C1)