Reputation: 3782
I need to calculate the power of 2 for the column p
using Spark 2.2 and Scala:
But if I do it this way, I get the error, because ($"ki" / $"ni")
is the column, not Double.
df.withColumn("p",(lit(1) - scala.math.pow(($"ki" / $"ni").as[Double],2))
Upvotes: 6
Views: 6944
Reputation: 475
POW function with sample data
val someDF = Seq(
(8,"A"),
(12,"B"),
(16,"C")).toDF("number","letter")
someDF.withColumn("pow_example",pow(2,$"number")).show()
Upvotes: 0
Reputation: 41957
you can use inbuilt pow function as
import org.apache.spark.sql.functions._
df.withColumn("power_of_two", pow($"p", lit(2)))
Upvotes: 9