【发布时间】:2020-11-23 20:49:51
【问题描述】:
我有两个数据框,我想检查 df1 是否包含 df2 中键为 a 和 b 的任何行,如果相等,则在 df2 中更改为 true,并添加 df1 中的新行,其中存在 False
df1
a | b | c | d
1 | 1 | 3 | 4
2 | 2 | 4 | 1
3 | 3 | 5 | 3
df2
a | b | c | d
1 | 1 | 4 | 5
4 | 4 | 3 | 2
这应该看起来像
df3
a | b | c | d | exists
1 | 1 | 4 | 5 | True
4 | 4 | 3 | 2 | False
1 | 1 | 3 | 4 | False
2 | 2 | 4 | 1 | False
3 | 3 | 5 | 3 | False
到目前为止我有这个
val newdf = df1.join(df2, df1("a")===df2("a") && df1("b") === df2("b"), "left")
.select(df2("a"), df2("b"),df2("c"),df2("d"),when(df2("a").isNull, false).otherwise(true).alias("exists"))
返回
a | b | c | d | exists
1 | 1 | 4 | 5 | True
rest of the rows are null
【问题讨论】:
-
1 | 1 | 3 | 4 | False这一行是否也会出现在 df3 中?因为 df1 中有匹配的行.. -
是的,两个数据框中的所有行都将在 df3 中。与 df2 匹配的那个将在 df3 中存在
标签: scala dataframe apache-spark