I have two dataframes as follows which I want to join based on col A
df1:
+------+--------+-------+
| A | B | C |
+------+--------+-------+
| a1 | 5 | asd |
| a2 | 12 | asd |
+------+--------+-------+
df2:
+------+--------+-------+
| A | B | D |
+------+--------+-------+
| a1 | 8 | qwe |
| a2 | 10 | qwe |
+------+--------+-------+
Since column B is same lets assume there is a logic of choosing between the two for example choose the
+------+--------+------+-----+
| A | B | C | D |
+------+--------+------+-----+
| a1 | 8 | asd | qwe |
| a2 | 12 | asd | qwe |
+------+--------+-------+----+
A simple way to achieve this is:
val _df1 = df1.withColumnRenamed("B","B_df1")
val _df2 = df2.withColumnRenamed("B", "B_df2)
_df1.join(_df2, Seq("A"))
.withColumn("B", when(col("B_df1") > col("B_df2"),
col("B_df1"))
.otherwise(col("B_df2"))
.drop(col("B_df1")
.drop("B_df2")
Is there a better way to achieve this without renaming and dropping columns?