title: pyspark groupBy and count across all columns
body: <p>I have a dataframe where every cell is a letter from A, B, C, or D. It is a very large dataframe. What's the most efficient way to come up with a count of the number of times each letter appears across the entire dataframe?</p>
  <p>A very small example:</p>
  <pre><code>col1    col2    col3        col4
  A       C       A               C
  B       D       C               A
  </code></pre>
  <p>I want a count that looks like:</p>
  <pre><code>col count   
  A       3      
  B       1      
  C       3       
  D       1
  </code></pre>
  <p>Does not have to be sorted.</p>
  <p>For example, I was thinking of combining the columns into one and calling groupby and count on that:</p>
  <pre><code>one_column_df = df.withColumn(&quot;mycol&quot;, array(&quot;col1&quot;, &quot; col2&quot;, &quot;col3&quot;, &quot;col4&quot;)).select(explode(col(&quot;mycol&quot;)))
  one_column_df.groupBy(&quot;col&quot;).count().show()
  </code></pre>
  <p>Is there a more efficient way than having to call array and explode?</p>