In Spark 2.x, you can create a new C5 column with the expression "C2 / C3 + C4" using withColumn() and org.apache.spark.sql.functions._ ,
val currentDf = Seq( ("steak", 1, 1, 150), ("steak", 2, 2, 180), ("fish", 3, 3, 100) ).toDF("C1", "C2", "C3", "C4") val requiredDf = currentDf .withColumn("C5", (col("C2")/col("C3")+col("C4")))
Alternatively, you can do the same with org.apache.spark.sql.Column . (But the complexity of the space in this approach is higher than when using org.apache.spark.sql.functions._ due to the creation of the Column object)
val requiredDf = currentDf .withColumn("C5", (new Column("C2")/new Column("C3")+new Column("C4")))
This worked great for me. I am using Spark 2.0.2.
Vidura mudalige
source share