[英]How to join(Merge) two SparkDataFrame in SparkR and keep one of the common columns
[英]Combine two columns in SparkR
在SparkR中合並兩列的簡單方法是什么? 考慮以下Spark DF:
salary_from salary_to position
1500 null a
null 1300 b
800 1000 c
我想將salary
列與這種邏輯結合起來。 從salary_from
和salary_to
取一個不為null的值,如果兩者都存在,則取一個中間值。
salary_from salary_to position salary
1500 null a 1500
null 1300 b 1300
800 1000 c 900
是否有辦法遍歷每一行並應用我的邏輯,就像我在R中使用apply方法一樣?
您可以使用coalesce
功能:
withColumn(
sdf, "salary",
expr("coalesce((salary_from + salary_to) / 2, salary_from, salary_to)")
)
返回第一個非空表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.