title: What is the correct way to calculate average using pyspark.sql functions?
body: <p>In pyspark dataframe, I have a timeseries of different events and I want to calculate the average count of events by month. What is the correct way to do that using the pyspark.sql functions?</p>
  
  <p>I have a feeling that this requires <em>agg</em>, <em>avg</em>, <em>window</em> partitioning, but I couldn't make it work.</p>
  
  <p>I have grouped the data by event and month and obtained something like this:</p>
  
  <pre><code>+------+-----+-----+
  | event|month|count|
  +------+-----+-----+
  |event1|    1| 1023|
  |event2|    1| 1009|
  |event3|    1| 1002|
  |event1|    2| 1012|
  |event2|    2| 1023|
  |event3|    2| 1017|
  |event1|    3| 1033|
  |event2|    3| 1011|
  |event3|    3| 1004|
  +------+-----+-----+
  </code></pre>
  
  <p>What I would like to have is this:</p>
  
  <pre><code>+------+-------------+
  | event|avg_per_month|
  +------+-------------+
  |event1|    1022.6666|
  |event2|    1014.3333|
  |event3|    1007.6666|
  +------+-------------+
  </code></pre>
  
  <p>What is the correct way to accomplish this?</p>