title: Counting unique ad impressions in Pandas
body: <p>I am working with a fairly large dataframe that includes three major columns:</p>
  
  <pre><code>df = pd.DataFrame({
           'ad_id': [2441, 102, 133, 102, 102, 2441, 105, 301],
           'user_id': [665, 152, 12, 665, 11, 238, 665, 12],
           'view_time': ['09/12/2017 10:11', '11/12/2017 10:22', '1/12/2017 10:22', '11/12/2017 10:35', '11/12/2017 13:01', '11/12/2017 14:11', '11/12/2017 14:38', '11/12/2017 14:52'],
  })
  </code></pre>
  
  <p>where <code>view_time</code>seems to be compatible with Pandas <code>datetime</code>.</p>
  
  <p>My goal here is to identify and print 4 ads with the highest number of unique views (that is, views by a particular user in 24h window where views recorded after the initial unique view do not count as unique), or, failing that, get <em>n</em> ads with the highest number of nonduplicate (different <code>view_time</code> values for any given user) views. </p>
  
  <p>I have tried to use the following:</p>
  
  <pre><code>df = df.groupby(by=['ad_id'], as_index=False).agg({'user_id': pd.Series.nunique, 'view_time': pd.Series.nunique})
  </code></pre>
  
  <p>but this seems to provide little help either in counting properly unique views or in sorting <code>ad_id</code> values on the basis of viewcount, however gross it might be.</p>