首页 > 解决方案 > 将 RDD 列转换为浮点数

问题描述

我刚刚ratingshttp://files.grouplens.org/datasets/movielens/ml-latest-small.zip将数据加载为 pySpark 中的 RDD。我想将评级列投射到float. 我该怎么做?

我尝试lambda在内部使用一个函数,map但它没有像我预期的那样工作。

下面是我试过的代码。

path_data = "/ml-latest-small"
ratingsFile = sc.textFile(path_data + "/ratings.csv")

ratingsFile_2 = ratingsFile.map(lambda x: x.split(","))

header = ratingsFile_2.first()

ratingsFile_3 = ratingsFile_2.filter(lambda x: x != header)

ratingsFile_4 = ratingsFile_3.map(lambda x: float(x[2])

ratingsFile_4.take(6)

我越来越

[4.0, 4.0, 4.0, 5.0, 5.0, 3.0]

代替

[['1', '1', 4.0, '964982703'],
 ['1', '3', 4.0, '964981247'],
 ['1', '6', 4.0, '964982224'],
 ['1', '47', 5.0, '964983815'],
 ['1', '50', 5.0, '964982931'],
 ['1', '70', 3.0, '964982400']]

标签: pythonpysparkrdd

解决方案


推荐阅读