pyspark: add a new field to a data frame Row element
<p>I have the following element:</p>

<pre><code>a = Row(ts=1465326926253, myid=u'1234567', mytype=u'good') 
</code></pre>

<p>The Row is of spark data frame Row class. Can I append a new field to a, so a would look like:</p>

<pre><code>a = Row(ts=1465326926253, myid=u'1234567', mytype=u'good', name = u'john') 
</code></pre>

<p>Thanks!</p>

<p>You cannot add new field to the <code>Row</code>. <code>Row</code> is a subclass of <code>tuple</code> and Python <code>tuples</code> are immutable. All you can do is create a new one:</p>

<pre><code>from pyspark.sql import Row

row = Row(ts=1465326926253, myid=u'1234567', mytype=u'good') 

Row(**row.asDict(), name=u"john")
## Row(myid='1234567', mytype='good', name='john', ts=1465326926253)
</code></pre>

<p>Please note that <code>Row</code> keeps it fields sorted by name.</p>

python apache-spark dataframe row pyspark