title: How to add a constant column in a Spark DataFrame?
body: <p>I want to add a column in a <code>DataFrame</code> with some arbitrary value (that is the same for each row). I get an error when I use <code>withColumn</code> as follows:</p>
  
  <pre><code>dt.withColumn('new_column', 10).head(5)
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-50-a6d0257ca2be&gt; in &lt;module&gt;()
        1 dt = (messages
        2     .select(messages.fromuserid, messages.messagetype, floor(messages.datetime/(1000*60*5)).alias("dt")))
  ----&gt; 3 dt.withColumn('new_column', 10).head(5)
  
  /Users/evanzamir/spark-1.4.1/python/pyspark/sql/dataframe.pyc in withColumn(self, colName, col)
     1166         [Row(age=2, name=u'Alice', age2=4), Row(age=5, name=u'Bob', age2=7)]
     1167         """
  -&gt; 1168         return self.select('*', col.alias(colName))
     1169 
     1170     @ignore_unicode_prefix
  
  AttributeError: 'int' object has no attribute 'alias'
  </code></pre>
  
  <p>It seems that I can trick the function into working as I want by adding and subtracting one of the other columns (so they add to zero) and then adding the number I want (10 in this case):</p>
  
  <pre><code>dt.withColumn('new_column', dt.messagetype - dt.messagetype + 10).head(5)
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>[Row(fromuserid=425, messagetype=1, dt=4809600.0, new_column=10),
   Row(fromuserid=47019141, messagetype=1, dt=4809600.0, new_column=10),
   Row(fromuserid=49746356, messagetype=1, dt=4809600.0, new_column=10),
   Row(fromuserid=93506471, messagetype=1, dt=4809600.0, new_column=10),
   Row(fromuserid=80488242, messagetype=1, dt=4809600.0, new_column=10)]
  </code></pre>
  
  <p>This is supremely hacky, right? I assume there is a more legit way to do this?</p>