如何在Spark中增加小数精度?

问题描述 投票:3回答:1

我有一个大型DataFrame,由~550列双精度和两列long(id)组成。正在从csv读入550列,我添加了两个id列。我对数据做的唯一其他事情是将一些csv数据从字符串更改为双精度(“Inf” - >“0”然后将列强制转换为double)并将NaN替换为0:

df = df.withColumn(col.name + "temp", 
                             regexp_replace(
                                 regexp_replace(df(col.name),"Inf","0")
                                 ,"NaN","0").cast(DoubleType))
df = df.drop(col.name).withColumnRenamed(col.name + "temp",col.name)
df = df.withColumn("timeId", monotonically_increasing_id.cast(LongType))
df = df.withColumn("patId", lit(num).cast(LongType))
df = df.na.fill(0)

当我计算时,我收到以下错误:

IllegalArgumentException: requirement failed: Decimal precision 6 exceeds max precision 5

有成千上万的行,我正在读取多个csvs的数据。如何提高小数精度?还有其他事情可以继续吗?当我读到一些csvs时,我只得到这个错误。他们可能有比其他小数更多的小数?

python scala apache-spark spark-dataframe bigdata
1个回答
7
投票

我认为错误是非常自我解释 - 你需要使用DecimalType而不是DoubleType

试试这个:

...
.cast(DecimalType(6)))

继续阅读:

https://spark.apache.org/docs/2.1.0/api/java/org/apache/spark/sql/types/DecimalType.html

http://spark.apache.org/docs/2.0.2/api/python/_modules/pyspark/sql/types.html

datatype for handling big numbers in pyspark

© www.soinside.com 2019 - 2024. All rights reserved.