title: How can I group RDD by key then count per unique string?
body: <p>I have an RDD like:</p>
  
  <pre><code>[(1, "Western"),
  (1, "Western")
  (1, "Drama")
  (2, "Western")
  (2, "Romance")
  (2, "Romance")]
  </code></pre>
  
  <p>I wish to count per userID the occurances of each movie genres resulting in</p>
  
  <pre><code>1, { "Western", 2), ("Drama", 1) } ... 
  </code></pre>
  
  <p>After that it's my intention to pick the one with the largest number and thus gaining the most popular genre per user. </p>
  
  <p>I have tried <code>userGenre.sortByKey().countByValue()</code>
  but to no avail I have no clue on how I can perform this task. I'm using pyspark jupyter notebook.</p>
  
  <p>EDIT:</p>
  
  <p>I have tried the following and it seems to have worked, could someone confirm?</p>
  
  <pre><code>userGenreRDD.map(lambda x: (x, 1)).aggregateByKey(\
                    0, # initial value for an accumulator \
                    lambda r, v: r + v, # function that adds a value to an accumulator \
                    lambda r1, r2: r1 + r2 # function that merges/combines two accumulators \
                   )
  </code></pre>