[英]pyspark RDD expand a row to multiple rows
我在pyspark中有以下RDD,我相信這應該很簡單,但是無法弄清楚:
information = [ (10, 'sentence number one'),
(17, 'longer sentence number two') ]
rdd = sc.parallelize(information)
我需要應用一個轉換,將RDD轉換為:
[ ('sentence', 10),
('number', 10),
('one', 10),
('longer', 17),
('sentence', 17),
('number', 17),
('two', 17) ]
基本上將句子鍵擴展為多行,並將單詞作為鍵。
我想避免使用SQL。
使用flatMap
:
rdd.flatMap(lambda x: [(w, x[0]) for w in x[1].split()])
示例 :
rdd.flatMap(lambda x: [(w, x[0]) for w in x[1].split()]).collect()
# [('sentence', 10), ('number', 10), ('one', 10), ('longer', 17), ('sentence', 17), ('number', 17), ('two', 17)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.