title: Pandas groupby apply function that combines some groups but not others
body: <p>I'm using pandas <code>groupby</code> on my DataFrame <code>df</code> which has columns <code>type</code>, <code>subtype</code>, and 11 others. I'm then calling an <code>apply</code> with my <code>combine_function</code> (needs a better name) on the groups like:</p>
  
  <pre><code>    grouped = df('type')
      reduced = grouped.apply(combine_function)
  </code></pre>
  
  <p>where my <code>combine_function</code> checks if any element in the group contains any element with the given subtype, say 1, and looks like:</p>
  
  <pre><code>def combine_function(group):
      if 1 in group.subtype:
          return aggregate_function(group)
      else:
          return group
  </code></pre>
  
  <p>The <code>combine_function</code> then can call an <code>aggregate_function</code>, that calculates summary statistics, stores them in the first row, and then sets that row to be the group. It looks like:</p>
  
  <pre><code>def aggregate_function(group):
      first = group.first_valid_index()
      group.value1[group.index == first] = group.value1.mean()
      group.value2[group.index == first] = group.value2.max()
      group.value3[group.index == first] = group.value3.std()
  
      group = group[(group.index == first)]
      return group
  </code></pre>
  
  <p>I'm fairly sure this isn't the best way to do this, but it has been giving my the desired results, 99.9% of the time on thousands of DataFrames. However it sometimes throws an error that is somehow related to a group that I don't want to aggregate has exactly 2 rows: </p>
  
  <pre><code>ValueError: Shape of passed values is (13,), indices imply (13, 5)
  </code></pre>
  
  <p>where my an example groups had size:</p>
  
  <pre><code>In [4]: grouped.size()
  Out[4]: 
  type
  1         9288
  3         7667
  5         7604
  11           2
  dtype: int64
  </code></pre>
  
  <p>It processed the 3 three fine, and then gave the error when it tried to combine everything. If I comment out the line <code>group = group[(group.index == first)]</code> so update but don't aggregate or call my <code>aggregate_function</code> on all groups its fine.</p>
  
  <p>Does anyone know the proper way to be doing this kind of aggregation of some groups but not others?</p>