如何关闭pyspark中的科学记数法?

How to turn off scientific notation in pyspark?

作为一些聚合的结果,我想出了以下 sparkdataframe:

 ------------+-----------------+-----------------+
|sale_user_id|gross_profit     |total_sale_volume|
+------------+-----------------+-----------------+
|       20569|       -3322960.0|     2.12569482E8|
|       24269|       -1876253.0|      8.6424626E7|
|        9583|              0.0|       1.282272E7|
|       11722|          18229.0|        5653149.0|
|       37982|           6077.0|        1181243.0|
|       20428|           1665.0|        7011588.0|
|       41157|          73227.0|        1.18631E7|
|        9993|              0.0|        1481437.0|
|        9030|           8865.0|      4.4133791E7|
|         829|              0.0|          11355.0|
+------------+-----------------+-----------------+

数据框的架构是:

root
 |-- sale_user_id: string (nullable = true)
 |-- tapp_gross_profit: double (nullable = true)
 |-- total_sale_volume: double (nullable = true)

如何在每个 gross_profit 和 total_sale_volume 列中禁用科学记数法?

最简单的方法是将双列转换为十进制,给出适当的 precision and scale:

df.withColumn('total_sale_volume', df.total_sale_volume.cast(DecimalType(18, 2)))

DecimalType 在 spark 3.0+

中被弃用

如果是string类型,先转Double类型,再转BigInt类型。无需设置精度:

df.withColumn('total_sale_volume', df.total_sale_volume.cast(StringType).cast(BigIntType))

或者无需导入:

df.withColumn('total_sale_volume', df.total_sale_volume.cast('string').cast('bigint'))