title: Spark DataFrame: Computing row-wise mean (or any aggregate operation)
body: <p>I have a Spark DataFrame loaded up in memory, and I want to take the mean (or any aggregate operation) over the columns. How would I do that? (In <code>numpy</code>, this is known as taking an operation over <code>axis=1</code>).</p>
  
  <p>If one were calculating the mean of the DataFrame down the rows (<code>axis=0</code>), then this is already built in:</p>
  
  <pre><code>from pyspark.sql import functions as F
  F.mean(...)
  </code></pre>
  
  <p>But is there a way to programmatically do this against the entries in the columns? For example, from the DataFrame below</p>
  
  <pre><code>+--+--+---+---+
  |id|US| UK|Can|
  +--+--+---+---+
  | 1|50|  0|  0|
  | 1| 0|100|  0|
  | 1| 0|  0|125|
  | 2|75|  0|  0|
  +--+--+---+---+
  </code></pre>
  
  <p>Omitting <code>id</code>, the means would be</p>
  
  <pre><code>+------+
  |  mean|
  +------+
  | 16.66|
  | 33.33|
  | 41.67|
  | 25.00|
  +------+
  </code></pre>