将数据帧架构从int更改为double的问题

时间:2018-03-29 10:15:11

标签: apache-spark pyspark spark-dataframe

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

数据框的架构是

label.printSchema

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

我正在使用的命令是

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

我收到的错误是:

error: unclosed character literal

2 个答案:

答案 0 :(得分:0)

label = label.withColumn(“value”,label(“value”)。cast(DoubleType))

答案 1 :(得分:-1)

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|
+-----+