title: PySpark - Adding a Column from a list of values
body: <p>I have to add column to a PySpark dataframe based on a list of values. </p>
  
  <pre><code>a= spark.createDataFrame([("Dog", "Cat"), ("Cat", "Dog"), ("Mouse", "Cat")],["Animal", "Enemy"])
  </code></pre>
  
  <p>I have a list called rating, which is a rating of each pet. </p>
  
  <pre><code>rating = [5,4,1]
  </code></pre>
  
  <p>I need to append the dataframe with a column called Rating, such that </p>
  
  <pre class="lang-none prettyprint-override"><code>+------+-----+------+
  |Animal|Enemy|Rating|
  +------+-----+------+
  |   Dog|  Cat|     5|
  |   Cat|  Dog|     4|
  | Mouse|  Cat|     1|
  +------+-----+------+
  </code></pre>
  
  <p>I have done the following however it is returning only the first value in the list in the Rating Column </p>
  
  <pre><code>def add_labels():
      return rating.pop(0)
  
  labels_udf = udf(add_labels, IntegerType())
  
  new_df = a.withColumn('Rating', labels_udf()).cache()
  </code></pre>
  
  <p>out:</p>
  
  <pre class="lang-none prettyprint-override"><code>+------+-----+------+
  |Animal|Enemy|Rating|
  +------+-----+------+
  |   Dog|  Cat|     5|
  |   Cat|  Dog|     5|
  | Mouse|  Cat|     5|
  +------+-----+------+
  </code></pre>