title: Calculate nunique() for groupby in pandas
body: <p>I have a dataframe with columns: </p>
  
  <ol>
  <li><code>diff</code> - difference between registration date and payment date,in days</li>
  <li><code>country</code> - country of user</li>
  <li><code>user_id</code></li>
  <li><code>campaign_id</code> -- another categorical column, we will use it in groupby</li>
  </ol>
  
  <p>I need to calculate count distinct users for every <code>country</code>+<code>campaign_id</code> group who has <code>diff</code>&lt;=n.
  For example, for <code>country</code> 'A', <code>campaign</code> 'abc' and <code>diff</code> 7 i need to get count distinct users from <code>country</code> 'A', <code>campaign</code> 'abc' and <code>diff</code> <strong>&lt;=</strong> 7</p>
  
  <p>My current solution(below) works too long</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  ## generate test dataframe
  df = pd.DataFrame({
          'country':np.random.choice(['A', 'B', 'C', 'D'], 10000),
          'campaign': np.random.choice(['camp1', 'camp2', 'camp3', 'camp4', 'camp5', 'camp6'], 10000),
          'diff':np.random.choice(range(10), 10000),
          'user_id': np.random.choice(range(1000), 10000)
          })
  ## main
  result_df = pd.DataFrame()
  for diff in df['diff'].unique():
      tmp_df = df.loc[df['diff']&lt;=diff,:]
      tmp_df = tmp_df.groupby(['country', 'campaign'], as_index=False).apply(lambda x: x.user_id.nunique()).reset_index()
      tmp_df['diff'] = diff
      tmp_df.columns=['country', 'campaign', 'unique_ppl', 'diff']
      result_df = pd.concat([result_df, tmp_df],ignore_index=True, axis=0)
  </code></pre>
  
  <p>Maybe there is better way to do this?</p>