如何在Apache Spark中添加多个列

编程入门 行业动态 更新时间:2024-10-12 20:26:58
本文介绍了如何在Apache Spark中添加多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的输入数据,其中四列以空格作为分隔符.我想添加第二列和第三列并打印结果

Here is my input data with four columns with space as the delimiter. I want to add the second and third column and print the result

sachin 200 10 2 sachin 900 20 2 sachin 500 30 3 Raju 400 40 4 Mike 100 50 5 Raju 50 60 6

我的代码处于中间状态

from pyspark import SparkContext sc = SparkContext() def getLineInfo(lines): spLine = lines.split(' ') name = str(spLine[0]) cash = int(spLine[1]) cash2 = int(spLine[2]) cash3 = int(spLine[3]) return (name,cash,cash2) myFile = sc.textFile("D:\PYSK\cash.txt") rdd = myFile.map(getLineInfo) print rdd.collect()

从这里我得到的结果是

[('sachin', 200, 10), ('sachin', 900, 20), ('sachin', 500, 30), ('Raju', 400, 40 ), ('Mike', 100, 50), ('Raju', 50, 60)]

现在我需要的最终结果如下,添加第二列和第三列并显示其余字段

Now the final result I need is as below, adding the 2nd and 3rd column and display the remaining fields

sachin 210 2 sachin 920 2 sachin 530 3 Raju 440 4 Mike 150 5 Raju 110 6

推荐答案

使用此:

def getLineInfo(lines): spLine = lines.split(' ') name = str(spLine[0]) cash = int(spLine[1]) cash2 = int(spLine[2]) cash3 = int(spLine[3]) return (name, cash + cash2, cash3)

更多推荐

如何在Apache Spark中添加多个列

本文发布于:2023-10-28 04:55:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何在   Apache   Spark

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!