title: Pyspark: obtain percentage result after groupBy
body: <p>For example, here is my test data</p>
  
  <pre><code>test = spark.createDataFrame([
      (0, 1, 5, "2018-06-03", "Region A"),
      (1, 1, 2, "2018-06-04", "Region B"),
      (2, 2, 1, "2018-06-03", "Region B"),
      (3, 3, 1, "2018-06-01", "Region A"),
      (3, 1, 3, "2018-06-05", "Region A"),
  ])\
    .toDF("orderid", "customerid", "price", "transactiondate", "location")
  test.show()
  </code></pre>
  
  <p>I can get summariztion data like this</p>
  
  <pre><code>test.groupBy("customerid", "location").agg(sum("price")).show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/obRZR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/obRZR.png" alt="enter image description here"></a></p>
  
  <p>but I also want the percentage data, something like this</p>
  
  <pre><code>+----------+--------+----------+ 
  |customerid|location|sum(price)| percentage
  +----------+--------+----------+ 
  |         1|Region B|         2|    20%
  |         1|Region A|         8|    80%
  |         3|Region A|         1|    100%
  |         2|Region B|         1|    100%
  +----------+--------+----------+
  </code></pre>
  
  <p>I want to know </p>
  
  <ul>
  <li>How can I do it? maybe using a window function?</li>
  <li>Can I pivot table turn it into something like this? (with percentage and sum column)</li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/eyFtC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eyFtC.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>I only find a pandas example at <a href="https://stackoverflow.com/questions/40658129/how-to-get-percentage-of-counts-of-a-column-after-groupby-in-pandas">How to get percentage of counts of a column after groupby in Pandas</a></p>
  
  <p>UPDATE:</p>
  
  <p>From the help of @Gordon Linoff, I can get the percentage by </p>
  
  <pre><code>from pyspark.sql.window import Window
  test.groupBy("customerid", "location").agg(sum("price"))\
    .withColumn("percentage", col("sum(price)")/sum("sum(price)").over(Window.partitionBy(test['customerid']))).show()
  </code></pre>