我試過只有兩種數據類型,包括十進制類型。請檢查您的數據幀,
>>> df.printSchema()
root
|-- col1: integer (nullable = true)
|-- col2: decimal(10,0) (nullable = true)
>>> df1.printSchema()
root
|-- col1: string (nullable = true)
|-- col2: string (nullable = true)
>>> df_schema = {field.name:field.dataType for field in df.schema.fields}
>>> dfSchema
{'col2': DecimalType(10,0), 'col1': IntegerType}
>>> df1_cols = df1.columns
>>> df1_cols
['col1', 'col2']
>>> for f in df1_cols:
df1 = df1.withColumn(f,df1[f].cast(df_schema[f]))
>>> df1.printSchema()
root
|-- col1: integer (nullable = true)
|-- col2: decimal(10,0) (nullable = true)
這是工作代碼 –