title: pandas dataframe: how to aggregate a subset of rows based on value of a column
body: <p>I have a pandas dataframe structured like this:</p>
  
  <pre><code>      value
  lab        
  A        50
  B        35
  C         8
  D         5
  E         1
  F         1
  </code></pre>
  
  <p>This is just an example, the actual dataframe is bigger, but follows the same structure.<br>
  The sample dataframe has been created with this two lines:</p>
  
  <pre><code>df = pd.DataFrame({'lab':['A', 'B', 'C', 'D', 'E', 'F'], 'value':[50, 35, 8, 5, 1, 1]})
  df = df.set_index('lab')
  </code></pre>
  
  <p>I would like to aggregate the rows whose value is smaller that a given threshold: all these rows should be substituted by a single row whose value is the sum of the substituted rows.</p>
  
  <p>For example, if I choose a threshold = 6, the expected result should be the following:</p>
  
  <pre><code>      value
  lab        
  A        50
  B        35
  C         8
  X         7 #sum of D, E, F
  </code></pre>
  
  <p>How can I do this?</p>
  
  <p>I thought to use <code>groupby()</code>, but all the examples I've seen involved the use of a separate column for grouping, so I do not know how to use it in this case.<br>
  I can select the rows smaller than my threshold with <code>loc</code>, by doing <code>df.loc[df['value'] &lt; threshold]</code> but I do not know how to sum only these rows and leave the rest of the dataframe unaltered.</p>