简体   繁体   中英

Scala DataFrame, copy value of non null column into a new column

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. Eg 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)

尝试使用when函数:

df.withColumn("Col", when(col("Col1").isNull(), col("Col2")).otherwise(col("Col1")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM