1

I have a dataframe, df2 such as:

ID | data
--------
1 | New
3 | New 
5 | New

and a main dataframe, df1:

ID | data | more
----------------
1 | OLD | a
2 | OLD | b
3 | OLD | c
4 | OLD | d
5 | OLD | e

I want to achieve something of the sort:

ID | data | more
----------------
1 | NEW | a
2 | OLD | b
3 | NEW | c
4 | OLD | d
5 | NEW | e

I want to update df1 based on df2, keeping the original values of df1 when they dont exist in df2.

Is there a fast way to do this than using isin? Isin is very slow when df1 and df2 are both very large.

1 Answer 1

3

With left join and "coalesce":

val df1 = Seq(
  (1, "OLD", "a"),
  (2, "OLD", "b"),
  (3, "OLD", "c"),
  (4, "OLD", "d"),
  (5, "OLD", "e")).toDF("ID", "data", "more")

val df2 = Seq(
  (1, "New"),
  (3, "New"),
  (5, "New")).toDF("ID", "data")

// action
val result = df1.alias("df1")
  .join(
    df2.alias("df2"),$"df2.ID" === $"df1.ID", "left")
  .select($"df1.ID",
    coalesce($"df2.data", $"df1.data").alias("data"),
    $"more")

Output:

+---+----+----+
|ID |data|more|
+---+----+----+
|1  |New |a   |
|2  |OLD |b   |
|3  |New |c   |
|4  |OLD |d   |
|5  |New |e   |
+---+----+----+
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to do this for dynamic columns and not listing all columns in the select statement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.