title: Pandas: Collapse first n rows in each group by aggregation
body: <p>I have a dataframe which is grouped by id.  There are many groups, and each group has a variable number of rows. The first three rows of all groups do not contain interesting data.  I would like to "collapse" the first three rows in each group to form a single row in the following way:  </p>
  
  <p>'id', and 'type' will remain the same in the new 'collapsed' row.<br>
  'grp_idx' will be renamed "0" when the aggregation of the first three rows occurs<br>
  col_1 will be the sum of the first three rows<br>
  col_2 will be the sum of the first three rows<br>
  The 'flag' in the "collapsed" row will be 0 if the values are all 0 in the first 3 rows.  'flag' will be 1 if it is 1 in any of the first three rows.  (A simple sum will suffice for this logic, since the flag is only set in one row for all groups)  </p>
  
  <p>Here is an example of what the dataframe looks like:</p>
  
  <pre><code>import pandas as pd
  import numpy as np   
  df = pd.DataFrame.from_items([
      ('id', [283,283,283,283,283,283,283,756,756,756]), 
      ('type', ['A','A','A','A','A','A','A','X','X','X']),
      ('grp_idx', [1,2,3,4,5,6,7,1,2,3]),
      ('col_1', [2,4,6,8,10,12,14,5,10,15]),
      ('col_2', [3,6,9,12,15,18,21,1,2,3]),
      ('flag', [0,0,0,0,0,0,1,0,0,1]),
      ]);
  print(df)
  
      id   type  grp_idx  col_1  col_2  flag
  0  283    A        1      2      3     0
  1  283    A        2      4      6     0
  2  283    A        3      6      9     0
  3  283    A        4      8     12     0
  4  283    A        5     10     15     0
  5  283    A        6     12     18     0
  6  283    A        7     14     21     1
  7  756    X        1      5      1     0
  8  756    X        2     10      2     0
  9  756    X        3     15      3     1
  </code></pre>
  
  <p>After processing, I expect the dataframe to look like:</p>
  
  <pre><code>ID  Type   grp_idx  col_1  col_2   flag
  283  A         0     12      18      0
  283  A         4     8       12      0
  283  A         5     10      15      0
  283  A         6     12      18      0
  283  A         7     14      21      1
  756  X         0     30       6      1
  </code></pre>
  
  <p>I'm not sure how to proceed.  I was trying to play around with</p>
  
  <p>df.groupby('id').head(3).sum()    </p>
  
  <p>but this is not doing what I need.  Any help, suggestions, code snippet would be really appreciated.</p>