问题描述
鉴于以下结构:
val df = Seq("Color", "Shape", "Range","Size").map(Tuple1.apply).toDF("color")
val df1 = df.withColumn("Success", when($"color"<=> "white", "Diamond").otherwise(0))
我想在上面的 size > 10 和 Shape 列中再写一个 WHEN 条件值为菱形,则应将钻石"值插入到其他 0 列中.我尝试如下,但失败了
I want to write one more WHEN condition at above where size > 10 and Shape columnvalue is Rhombus then "Diamond" value should be inserted to the column else 0. I tried like below but it's failing
val df1 = df.withColumn("Success", when($"color" <=> "white", "Diamond").otherwise(0)).when($"size">10)
请建议我只使用带有 Scala 的数据框选项.带有 sqlContext 的 Spark-SQL 对我来说没有帮助.
Please suggest me with only dataframe option with scala. Spark-SQL with sqlContext is not helpful idea for me.
谢谢!
推荐答案
您可以链接 when
类似于 https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/Column.html#when-org.apache.spark.sql.Column-java.lang.Object-自 (1.4.0) 起可用
You can chain the when
similar to the example in https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/Column.html#when-org.apache.spark.sql.Column-java.lang.Object-available since (1.4.0)
// Scala:
people.select(when(people("gender") === "male", 0)
.when(people("gender") === "female", 1)
.otherwise(2))
你的例子:
val df1 = df.withColumn("Success",
when($"color" <=> "white", "Diamond")
.when($"size" > 10 && $"shape" === "Rhombus", "Diamond")
.otherwise(0))
这篇关于Apache spark case 在不同的列上有多个 when 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!