title: How to use Pandas rolling with groupby and removing duplicates
body: <p>I have a table like this:</p>
  <pre><code>date    ID    Value
  1       aa     5.5
  1       aa     5.5
  1       bb     66
  1       bb     66
  2       cc     2.03
  2       aa     0.1
  2       aa     0.1
  3       bb     7
  4       dd     7
  5       aa     4
  5       aa     4
  </code></pre>
  <p>Some information about rows:</p>
  <pre><code>Date - Same date can appear in more than one row
  ID - Same ID can appear in more than one row
  If ID and Date are same, then value will also be same.
  </code></pre>
  <p>I want to calculate <code>rolling().mean()</code> of <code>Value</code> column. But I want to groupby <code>date</code> and <code>ID</code>, and calculate the rolling mean that, it does not take the mean of row with same date.</p>
  <p>So this do not work, because in this, it takes rolling mean of same date twice.</p>
  <p><code>df.groupby([&quot;ID&quot;,&quot;date&quot;])[&quot;Value&quot;].rolling(3).mean()</code></p>
  <p>I have implemented a for-loop solution but it is way slower, and I am working on millions of rows. This is my current solution.</p>
  <pre><code>uniqueID = df[&quot;ID&quot;]
  for idname in uniqueID:
      temp = df.loc[df[&quot;ID&quot;] == idname].drop_duplicates(inplace=False,keep=&quot;first&quot;,subset=&quot;date&quot;).set_index('date', inplace=False)[&quot;Value&quot;].rolling(3).mean()
      df.loc[df[&quot;ID&quot;] == idname,&quot;rollingmeanCol&quot;] = pd.merge(df.loc[df[&quot;ID&quot;]==idname,[&quot;date&quot;]],temp,on=[&quot;date&quot;],how=&quot;left&quot;)[&quot;Value&quot;].values
  </code></pre>
  <p>Any faster solution without loops?</p>
  <p>In the loop also, I am doing this query 3 times, any way to do this query 1 time only? <code>df.loc[df[&quot;ID&quot;] == idname]</code></p>
  <p>Expected output (can be verified using the above loop code)</p>
  <pre><code>date   ID   Value   rollingmeanCol
  1   aa     5.5        NaN
  1   aa     5.5        NaN
  1   bb     66.0       NaN
  1   bb     66.0       NaN
  2   cc     2.03       NaN
  2   aa     0.1        NaN
  2   aa     0.1        NaN
  3   bb     7.0        NaN
  4   dd     7.0        NaN
  5   aa     4.0        3.1999999999999997
  5   aa     4.0        3.1999999999999997
  </code></pre>