python - 将 RDD 列转换为浮点数
问题描述
我刚刚ratings
从http://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']]
解决方案
推荐阅读
- android - Undertsrand OnTouchListener 事件
- javascript - 为什么我没有得到 dropdownitemlist2 选择的值?
- java - JTable 未连接到 MS-Access 数据库
- laravel - 如何为长时间运行的作业设置自定义 retry_after | 拉拉维尔
- java - 我如何使用hibernate并行插入多个不相关的表?
- excel - VBA:将特定单元格从多个工作簿(具有多个工作表)复制到单个工作簿
- actions-on-google - 如何通过订阅为 Actions on Google 启用完整版
- http - HTTP GET 请求总是返回 301
- mongodb - MongoDB跳过管道上的阶段?
- c - gcc 未定义对 portaudio 函数的引用