【发布时间】:2020-08-24 08:10:07
【问题描述】:
我有以下 RDD 的行。可以看出每个字段都是字符串类型
[Row(A='6', B='1', C='hi'),
Row(A='4', B='5', C='bye'),
Row(A='8', B='9', C='night')]
我想将此 RDD 转换为具有 IntegerTypes 列 A 和 B 的数据框
dtypes = [
StructField('A', IntegerType(), True),
StructField('B', IntegerType(), True),
StructField('C', StringType(), True)
]
df = spark.createDataFrame(rdd, StructType(dtypes))
我收到以下错误:
TypeError: field A: IntegerType can not accept
object '6' in type <class 'str'>
我怎样才能成功地将 '6' 转换为 IntegerType?
【问题讨论】:
-
我看到了那个帖子。它直接处理在 spark DF 中转换列类型,而不是在从 RDD 创建数据帧时转换列类型
-
好的,您需要修改行的 RDD,以便在创建数据帧之前将所有这些字符串数据转换为整数。
标签: python apache-spark pyspark schema