title: Pandas: Replace a set of rows by their sum
body: <p>I'm sure there is a neat way of doing this, but haven't had any luck finding it yet.</p>
  
  <p>Suppose I have a data frame: </p>
  
  <pre><code>f = pd.DataFrame({'A':[1, 2, 3, 4], 'B': [10, 20, 30, 40], 'C':[100, 200, 300, 400]}).T
  </code></pre>
  
  <p>that is, with rows indexed A, B and C. </p>
  
  <p>Now suppose I want to take rows A and B, and replace them both by a single row that is their sum; and, moreover, that I want to assign a given index (say 'sum') to that replacement row (note the order of indices doesn't matter). </p>
  
  <p>At the moment I'm having to do:</p>
  
  <pre><code>f.append(pd.DataFrame(f.ix[['A','B']].sum()).T).drop(['A','B'])
  </code></pre>
  
  <p>followed by something equally clunky to set the index of the replacement row. However, I'm curious to know if there's an elegant, one-line way of doing both of these steps?</p>