title: PySpark Count Distinct By Group In A RDD
body: <p>I have an RDD of date-time and hostname as <code>tuple</code> and I want to count the unique hostnames by date. </p>
  
  <p><strong>RDD:</strong></p>
  
  <pre><code>X = [(datetime.datetime(1995, 8, 1, 0, 0, 1), u'in24.inetnebr.com'),
       (datetime.datetime(1995, 8, 1, 0, 0, 7), u'uplherc.upl.com'),
       (datetime.datetime(1995, 8, 1, 0, 0, 8), u'uplherc.upl.com'),
       (datetime.datetime(1995, 8, 2, 0, 0, 8), u'uplherc.upl.com'),
       (datetime.datetime(1995, 8, 2, 0, 0, 8), u'uplherc.upl.com'),
       (datetime.datetime(1995, 8, 2, 0, 0, 9), u'ix-esc-ca2-07.ix.netcom.com'),
       (datetime.datetime(1995, 8, 3, 0, 0, 10), u'uplherc.upl.com'),
       (datetime.datetime(1995, 8, 3, 0, 0, 10), u'slppp6.intermind.net'),
       (datetime.datetime(1995, 8, 4, 0, 0, 10), u'piweba4y.prodigy.com'),
       (datetime.datetime(1995, 8, 5, 0, 0, 11), u'slppp6.intermind.net')]
  </code></pre>
  
  <p><strong>DESIRED OUTPUT:</strong></p>
  
  <pre><code>[(datetime.datetime(1995, 8, 1, 0, 0, 1), 2),
   (datetime.datetime(1995, 8, 2, 0, 0, 8), 2),
   (datetime.datetime(1995, 8, 3, 0, 0, 10), 2),
   (datetime.datetime(1995, 8, 4, 0, 0, 10), 1),
   (datetime.datetime(1995, 8, 5, 0, 0, 11), 1)]
  </code></pre>
  
  <p><strong>MY ATTEMPT:</strong></p>
  
  <pre><code>dayGroupedHosts = X.groupBy(lambda x: x[0]).distinct()
  dayHostCount = dayGroupedHosts.count()
  </code></pre>
  
  <p>I am getting an error while performing <code>count</code> operation. I am new to <code>Spark</code> and I would like to know the correct and efficient <code>transformation</code> to achieve such tasks. </p>
  
  <p>Thanks a lot in advance. </p>