如何计算DataFrame列的2的幂(How to calculate the power of 2 for the column of DataFrame)

编程入门 行业动态 更新时间:2024-10-19 11:35:39
如何计算DataFrame列的2的幂(How to calculate the power of 2 for the column of DataFrame)

我需要使用Spark 2.2和Scala计算列p的2的幂:

但如果我这样做,我得到错误,因为($"ki" / $"ni")是列,而不是Double。

df.withColumn("p",(lit(1) - scala.math.pow(($"ki" / $"ni").as[Double],2))

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))

最满意答案

你可以使用内置的pow功能

import org.apache.spark.sql.functions._ df.withColumn("power_of_two", pow($"p", lit(2)))

you can use inbuilt pow function as

import org.apache.spark.sql.functions._ df.withColumn("power_of_two", pow($"p", lit(2)))

更多推荐

本文发布于:2023-08-04 11:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:calculate   DataFrame   column   power

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!