title: Get group id back into pandas dataframe
body: <p>For dataframe</p>
  
  <pre><code>In [2]: df = pd.DataFrame({'Name': ['foo', 'bar'] * 3,
     ...:                    'Rank': np.random.randint(0,3,6),
     ...:                    'Val': np.random.rand(6)})
     ...: df
  Out[2]: 
    Name  Rank       Val
  0  foo     0  0.299397
  1  bar     0  0.909228
  2  foo     0  0.517700
  3  bar     0  0.929863
  4  foo     1  0.209324
  5  bar     2  0.381515
  </code></pre>
  
  <p>I'm interested in grouping by Name and Rank and possibly getting aggregate values</p>
  
  <pre><code>In [3]: group = df.groupby(['Name', 'Rank'])
  In [4]: agg = group.agg(sum)
  In [5]: agg
  Out[5]: 
                  Val
  Name Rank          
  bar  0     1.839091
       2     0.381515
  foo  0     0.817097
       1     0.209324
  </code></pre>
  
  <p>But I would like to get a field in the original <code>df</code> that contains the group number for that row, like</p>
  
  <pre><code>In [13]: df['Group_id'] = [2, 0, 2, 0, 3, 1]
  In [14]: df
  Out[14]: 
    Name  Rank       Val  Group_id
  0  foo     0  0.299397         2
  1  bar     0  0.909228         0
  2  foo     0  0.517700         2
  3  bar     0  0.929863         0
  4  foo     1  0.209324         3
  5  bar     2  0.381515         1
  </code></pre>
  
  <p>Is there a good way to do this in pandas?</p>
  
  <p>I can get it with python, </p>
  
  <pre><code>In [16]: from itertools import count
  In [17]: c = count()
  In [22]: group.transform(lambda x: c.next())
  Out[22]: 
     Val
  0    2
  1    0
  2    2
  3    0
  4    3
  5    1
  </code></pre>
  
  <p>but it's pretty slow on a large dataframe, so I figured there may be a better built in pandas way to do this. </p>