将RDD柱浇铸到F

2024-04-29 10:01:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚将http://files.grouplens.org/datasets/movielens/ml-latest-small.zip中的ratings数据作为RDDs加载到pySpark中。我想把收视率列转换成float。我该怎么做

我尝试在map中使用lambda函数,但它没有像我预期的那样工作

下面是我尝试的代码

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']]

Tags: pathlambdaorghttpmapdatafilesfloat