[英]cast string column to decimal in scala dataframe
I have a dataframe (scala) I am using both pyspark and scala in a notebook我有一个数据框 (scala) 我在笔记本中同时使用 pyspark 和 scala
#pyspark
spark.read.csv(output_path + '/dealer', header = True).createOrReplaceTempView('dealer_dl')
%scala
import org.apache.spark.sql.functions._
val df = spark.sql("select * from dealer_dl")
How to convert a string column (amount) into decimal in scala dataframe.如何在 Scala 数据框中将字符串列(数量)转换为十进制。
I tried as below.我试过如下。
%scala
df = df.withColumn("amount", $"amount".cast(DecimalType(9,2)))
But I am getting an error as below:但我收到如下错误:
error: reassignment to val错误:重新分配给 val
I am used to pyspark and quite new to scala.我习惯了 pyspark 并且对 Scala 很陌生。 I need to do by scala to proceed further.我需要通过 Scala 来做进一步的工作。 Please let me know.请告诉我。 Thanks.谢谢。
in scala you can't reasign references defined as val
but val
is immutable reference.在 scala 中,您不能重新分配定义为val
引用,但val
是不可变的引用。 if you want to use reasigning some ref you can use var
but better solution is not reasign something to the same reference name and use another val
.如果您想使用重新分配一些引用,您可以使用var
但更好的解决方案不是将某些内容重新分配给相同的引用名称并使用另一个val
。 For example:例如:
val dfWithDecimalAmount = df.withColumn("amount", $"amount".cast(DecimalType(9,2)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.