hive 将列的科学记数法转换为十进制值

px9o7tmv  于 2023-10-18  发布在  Hive
关注(0)|答案(1)|浏览(230)

我在一栏中有以下数据
| 值|
| --|
| 1.873452634567E7 |
| null|
| 1.87345265634544467E9 |
| 1.8734526563456347E10 |
| 1.8734526563456723E8 |
我试

  1. df.withColumn("s", 'value.cast("Decimal(14,4)"))

但这并没有帮助。

wdebmtf2

wdebmtf21#

如果你先施放浮动,然后施放Decadition,你会得到你需要的。

  1. from pyspark.sql import SparkSession
  2. from pyspark.sql.functions import col
  3. # Create a Spark session
  4. spark = SparkSession.builder.appName("ScientificToDecimal").getOrCreate()
  5. # Sample data
  6. data = [("1.873452634567E7",),
  7. (None,),
  8. ("1.87345265634544467E9",),
  9. ("1.8734526563456347E10",),
  10. ("1.8734526563456723E8",)]
  11. # Define schema and create a DataFrame
  12. columns = ["value"]
  13. df = spark.createDataFrame(data, columns)
  14. # Convert to float first, then to decimal
  15. df = df.withColumn("value_float", col("value").cast("float"))
  16. df = df.withColumn("value_decimal", col("value_float").cast("Decimal(38,4)"))
  17. # Show results
  18. df.show()
展开查看全部

相关问题