小编gsm*_*113的帖子

如何将 Scala 数据框中的所有十进制列转换为双精度类型?

我有一个十进制和字符串类型的数据框。我想将所有十进制列转换为 double 而不命名它们。我试过这个没有成功。有点新的火花。

>df.printSchema

root

 |-- var1: decimal(38,10) (nullable = true)
 |-- var2: decimal(38,10) (nullable = true)
 |-- var3: decimal(38,10) (nullable = true)
…
150 more decimal and string columns
Run Code Online (Sandbox Code Playgroud)

我尝试:

import org.apache.spark.sql.types._

val cols = df.columns.map(x => {
    if (x.dataType == DecimalType(38,0)) col(x).cast(DoubleType) 
    else col(x)
})
Run Code Online (Sandbox Code Playgroud)

我得到

<console>:30: error: value dataType is not a member of String
           if (x.dataType == DecimalType(38,0)) col(x).cast(DoubleType)
Run Code Online (Sandbox Code Playgroud)

scala apache-spark apache-spark-sql

0
推荐指数
1
解决办法
3028
查看次数

标签 统计

apache-spark ×1

apache-spark-sql ×1

scala ×1