在 Scala 中从数据框中添加两列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40041499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding two columns from a data frame in scala
提问by Nirmal
I have two columns age and salary stored in DF. I just want to write a scala code to add these values column wise. i tried
我有两列存储在 DF 中的年龄和薪水。我只想编写一个 Scala 代码来明智地添加这些值。我试过
val age_1 = df.select("age")
val salary_1=df.select("salary")
val add = age_1+salary_1
gives me error. please help
给我错误。请帮忙
回答by mrsrinivas
In the following sparkis an instance of SparkSession, so the import has to come afterthe instantiation of spark.
下面spark是 的一个实例SparkSession,因此导入必须在的实例化之后进行spark。
$-notationcan be used here by importing spark implicits with
$-notation可以通过导入 spark 隐式在这里使用
import spark.implicits._
then use $-notation
然后使用 $-notation
val add = df.select($"age" + $"salary")
final scala code:
最终的Scala代码:
import spark.implicits._
val add = df.select($"age" + $"salary")

