我有两个数据帧:
DF1:
ID | Col1 | Col2
1 a aa
2 b bb
3 c cc
DF2:
ID | Col1 | Col2
1 ab aa
2 b bba
4 d dd我如何加入这两个外勤部,结果应该是:
Result:
1 ab aa
2 b bba
3 c cc
4 d dd我的代码是:
val df = DF1.join(DF2, Seq("ID"), "outer")
.select($"ID",
when(DF1("Col1").isNull, lit(0)).otherwise(DF1("Col1")).as("Col1"),
when(DF1("Col2").isNull, lit(0)).otherwise(DF2("Col2")).as("Col2"))
.orderBy("ID")它可以工作,但我不想指定每一列,因为我有大型文件。那么,在不指定每一列的情况下,是否有任何方法来更新dataframe (如果在第二个DF中添加了一些新的)?
发布于 2018-07-06 12:05:05
一个简单的leftanti将df1与df2连接起来,并将结果合并到df2中,应该得到您想要的输出如下
df2.union(df1.join(df2, Seq("ID"), "leftanti")).orderBy("ID").show(false)这应该会给你
+---+----+----+
|ID |Col1|Col2|
+---+----+----+
|1 |ab |aa |
|2 |b |bba |
|3 |c |cc |
|4 |d |dd |
+---+----+----+解决方案与代码中的逻辑不匹配,但会生成预期的结果
https://stackoverflow.com/questions/51209665
复制相似问题