【问题标题】:Issue in changing the dataframe schema from int to double将数据框架构从 int 更改为 double 的问题
【发布时间】:2018-03-29 10:15:10
【问题描述】:

我有一个数据框标签,我想将数据框的架构从整数更改为双精度

数据框的架构是

label.printSchema

root |-- value: integer (nullable = false)

我使用的命令是

label = label.withColumn('value', label.value.cast('double'))

我收到的错误是:

error: unclosed character literal

【问题讨论】:

  • sparql != 火花
  • 你是否从 pyspark.sql.types 导入 DoubleType
  • 我做了但没有发生我只是想将现有数据帧的架构从 int 更改为 double ,请告诉我该怎么做

标签: apache-spark pyspark spark-dataframe


【解决方案1】:

label = label.withColumn("value", label("value").cast(DoubleType))

【讨论】:

    【解决方案2】:
    from pyspark.sql.types import DoubleType,IntegerType
    cSchema = StructType([StructField("value",IntegerType())])
    test_list = [[1],[2]]
    df = spark.createDataFrame(test_list,schema=cSchema) 
    df.printSchema()
    castedDF = df.withColumn("value", df["value"].cast("double"))
    castedDF.printSchema()
    castedDF.show()
    

    而且,输出是(如预期的那样)

    root
     |-- value: integer (nullable = true)
    root
     |-- value: double (nullable = true)
    +-----+
    |value|
    +-----+
    |  1.0|
    |  2.0|
    +-----+
    

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      相关资源
      最近更新 更多