Error using reducebykey: int object is unsubscriptable

后端 未结 2 523
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 17:40

I\'m getting an error \"int object is unsubscriptable\" while executing the following script :

element.reduceByKey( lambda x , y : x[1]+y[1]         


        
2条回答
  •  孤街浪徒
    2021-01-03 18:13

    Another approach would be to use Dataframe

    rdd = sc.parallelize([('A', ('toto', 10)),('A', ('titi', 30)),('5', ('tata', 10)),('A', ('toto', 10))])
    rdd.map(lambda (a,(b,c)): (a,b,c)).toDF(['a','b','c']).groupBy('a').agg(sum("c")).rdd.map(lambda (a,c): (a,c)).collect()
    
    >>>[(u'5', 10), (u'A', 50)]
    

提交回复
热议问题