0

I have a Scala DataFrame and want to add a new column to it such that its value is based on any of the two columns which is non null. E.g. in the below table, col1 and col2 are the columns that I want to add the new column based off of, such that if col1 is null then the new column has value from col2 and vice versa for col2. One of col1 and col2 will always have a non null value. I need to do this only in Scala without using SQL. Thank you for any help in advance.

<table><tbody><tr><th>Col1</th><th>Col2</th><th>Col3</th></tr><tr><td>11</td><td>null</td><td>Value13</td></tr><tr><td>null</td><td>22</td><td>Value23</td></tr><tr><td>32</td><td>32</td><td>Value33</td></tr></tbody></table>

This is the best I could try but it won't work.

df.withColumn("Col", df.Col1.isNull() ? df.Col2: df.Col1)
1

1 Answer 1

3

Try using when function:

df.withColumn("Col", when(col("Col1").isNull(), col("Col2")).otherwise(col("Col1")))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Vladislav, I just found the answer on another post but have upvoted your answer :) It won't show though as I don't have enough rep.

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.