title: grouping pandas dataframe by two columns (or more)?
body: <p>I have the following dataframe:</p>
  
  <pre><code>mydf = pandas.DataFrame({"cat": ["first", "first", "first", "second", "second", "third"], "class": ["A", "A", "A", "B", "B", "C"], "name": ["a1", "a2", "a3", "b1", "b2", "c1"], "val": [1,5,1,1,2,10]})
  </code></pre>
  
  <p>I want to create a dataframe that makes summary statistics about the <code>val</code> column of items with the same <code>class</code> id. For this I use <code>groupby</code> as follows:</p>
  
  <pre><code>mydf.groupby("class").val.sum()
  </code></pre>
  
  <p>that's the correct behavior, but I'd like to retain the <code>cat</code> column information in the resulting df. can that be done? do I have to <code>merge/join</code> that info in later? I tried:</p>
  
  <pre><code>mydf.groupby(["cat", "class"]).val.sum()
  </code></pre>
  
  <p>but this uses hierarchical indexing. I'd like to have a plain dataframe back that just has the <code>cat</code> value for each group, where the group by is <code>class</code>. The output should be a dataframe (not series) with the values of cat and class, where the <code>val</code> entries are summed over each entry that has the same <code>class</code>:</p>
  
  <pre><code>cat     class    val
  first   A         7
  second  B         3
  third   C        10
  </code></pre>
  
  <p>is this possible?</p>