title: Efficiently adding calculated rows based on index values to a pandas DataFrame
body: <p>I have a pandas DataFrame in the following format:</p>
  
  <pre><code>     a   b   c
  0    0   1   2
  1    3   4   5
  2    6   7   8
  3    9  10  11
  4   12  13  14
  5   15  16  17
  </code></pre>
  
  <p>I want to append a calculated row that performs some math based on a given items index value, e.g. adding a row that sums the values of all items with an index value &lt; 2, with the new row having an index label of 'Red'. Ultimately, I am trying to add three rows that group the index values into categories:</p>
  
  <ul>
  <li>A row with the sum of item values where index value are &lt; 2, labeled as 'Red'</li>
  <li>A row with the sum of item values where index values are 1 &lt; x &lt; 4, labeled as 'Blue'</li>
  <li>A row with the sum of item values where index values are > 3, labeled as 'Green'</li>
  </ul>
  
  <p>Ideal output would look like this:</p>
  
  <pre><code>       a   b   c
  0      0   1   2
  1      3   4   5
  2      6   7   8
  3      9  10  11
  4     12  13  14
  5     15  16  17
  Red    3   5   7
  Blue  15  17  19
  Green 27  29  31
  </code></pre>
  
  <p>My current solution involves transposing the DataFrame, applying a map function for each calculated column and then re-transposing, but I would imagine pandas has a more efficient way of doing this, likely using <code>.append()</code>.</p>
  
  <p>EDIT: 
  My in-elegant pre-set list solution (originally used <code>.transpose()</code> but  I improved it using <code>.groupby()</code> and <code>.append()</code>):</p>
  
  <pre><code>df = pd.DataFrame(np.arange(18).reshape((6,3)),columns=['a', 'b', 'c'])
  df['x'] = ['Red', 'Red', 'Blue', 'Blue', 'Green', 'Green']
  df2 = df.groupby('x').sum()
  df = df.append(df2)
  del df['x']
  </code></pre>
  
  <p>I much prefer the flexibility of BrenBarn's answer (see below).</p>