pyspark:向数据框的Row元素添加一个新字段 [英] pyspark: add a new field to a data frame Row element
本文介绍了pyspark:向数据框的Row元素添加一个新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下元素:
a = Row(ts=1465326926253, myid=u'1234567', mytype=u'good')
该行属于火花数据框的行类.我可以在a后面附加一个新字段,这样a会像这样:
The Row is of spark data frame Row class. Can I append a new field to a, so a would look like:
a = Row(ts=1465326926253, myid=u'1234567', mytype=u'good', name = u'john')
谢谢!
推荐答案
此处是有效的更新答案.首先,您必须创建字典,然后更新字典,然后将其写到pyspark行中.
Here is an updated answer that works. First you have to create a dictionary then update the dict and then write it out to a pyspark Row.
代码如下:
from pyspark.sql import Row
#Creating the pysql row
row = Row(field1=12345, field2=0.0123, field3=u'Last Field')
#Convert to python dict
temp = row.asDict()
#Do whatever you want to the dict. Like adding a new field or etc.
temp["field4"] = "it worked!"
# Save or output the row to a pyspark rdd
output = Row(**temp)
#How it looks
output
In [1]:
Row(field1=12345, field2=0.0123, field3=u'Last Field', field4='it worked!')
这篇关于pyspark:向数据框的Row元素添加一个新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文