title: returning aggregated dataframe from pandas groupby
body: <p>I'm trying to wrap my head around Pandas groupby methods. I'd like to write a function that does some aggregation functions and then returns a Pandas DataFrame. Here's a grossly simplified example using sum(). I know there are easier ways to do simple sums, in real life my function is more complex:</p>
  
  <pre><code>import pandas as pd
  df = pd.DataFrame({'col1': ['A', 'A', 'B', 'B'], 'col2':[1.0, 2, 3, 4]})
  
  In [3]: df
  Out[3]: 
    col1  col2
  0    A     1
  1    A     2
  2    B     3
  3    B     4
  
  def func2(df):
      dfout = pd.DataFrame({ 'col1' : df['col1'].unique() ,
                             'someData': sum(df['col2']) })
      return  dfout
  
  t = df.groupby('col1').apply(func2)
  
  In [6]: t
  Out[6]: 
         col1  someData
  col1                 
  A    0    A         3
  B    0    B         7
  </code></pre>
  
  <p>I did not expect to have <code>col1</code> in there twice nor did I expect that mystery index looking thing. I really thought I would just get <code>col1</code> &amp; <code>someData</code>. </p>
  
  <p>In my real life application I'm grouping by more than one column and really would like to get back a DataFrame and not a Series object.<br>
  Any ideas for a solution or an explanation on what Pandas is doing in my example above?</p>
  
  <p><strong>----- added info -----</strong></p>
  
  <p>I should have started with this example, I think:</p>
  
  <pre><code>In [13]: import pandas as pd
  
  In [14]: df = pd.DataFrame({'col1':['A','A','A','B','B','B'], 'col2':['C','D','D','D','C','C'], 'col3':[.1,.2,.4,.6,.8,1]})
  
  In [15]: df
  Out[15]: 
    col1 col2  col3
  0    A    C   0.1
  1    A    D   0.2
  2    A    D   0.4
  3    B    D   0.6
  4    B    C   0.8
  5    B    C   1.0
  
  In [16]: def func3(df):
     ....:         dfout =  sum(df['col3']**2)
     ....:         return  dfout
     ....: 
  
  In [17]: t = df.groupby(['col1', 'col2']).apply(func3)
  
  In [18]: t
  Out[18]: 
  col1  col2
  A     C       0.01
        D       0.20
  B     C       1.64
        D       0.36
  </code></pre>
  
  <p>In the above illustration the result of the <code>apply()</code> function is a Pandas Series. And it lacks the groupby columns from the <code>df.groupby</code>. The essence of what I'm struggling with is how do I create a function which I apply to a groupby which returns both the result of the function AND the columns on which it was grouped? </p>
  
  <p><strong>----- yet another update ------</strong></p>
  
  <p>It appears that if I then do this:</p>
  
  <pre><code> pd.DataFrame(t).reset_index()
  </code></pre>
  
  <p>I get back a dataframe which is really close to what I was after. </p>