我有一个十进制和字符串类型的数据框。我想将所有十进制列转换为 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)