【发布时间】:2017-12-01 16:45:10
【问题描述】:
我正在尝试将我的 RDD 键值对中的列 [2] 值从字符串转换为整数,以便能够将它们相加并计算平均值。
我试图在映射阶段让列 [2] 自动显示为整数,但出现错误“TypeError: 'type' object has no attribute 'getitem'”
textfile = sc.textFile("hdfs://...csv")
test = textfile.map(lambda x: (x.split(",")[1], x.split(",")(int[2]))
在 PySpark RDD 中将 column[2] 值转换/映射为整数的正确方法是什么?
【问题讨论】:
-
您的错误是因为
int[2]- 您试图从数据类型中获取第三项,这没有意义(这就是错误消息所说的)。我想你的意思是写:test = textfile.map(lambda x: (x.split(",")[1], int(x.split(",")[2])),即将x.split(",")中的第二个元素转换为int。另请记住,python 是 0 索引的,因此您编写的代码假定len(x.split(",")) >= 3 -
完美运行,谢谢!
标签: python type-conversion integer pyspark rdd