Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the power of 2 for the column of DataFrame

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))
like image 494
Markus Avatar asked Jan 09 '18 17:01

Markus


1 Answers

you can use inbuilt pow function as

import org.apache.spark.sql.functions._
df.withColumn("power_of_two", pow($"p", lit(2)))
like image 125
Ramesh Maharjan Avatar answered Oct 06 '22 21:10

Ramesh Maharjan