title: Pandas difference between first and last grouped by consecutive events
body: <p>I have a dataframe containing open/close, candle color and number of consecutive candles. </p>
  
  <pre><code>    date open close color  run
  00:01:00  100   102     g    1
  00:02:00  102   104     g    2
  00:03:00  104   106     g    3
  00:04:00  106   105     r    1
  00:05:00  105   101     r    2
  00:06:00  101   102     g    1  
  00:06:00  102   103     g    2 
  </code></pre>
  
  <p>I'm trying to calculate the absolute value of the difference between the open of the first candle in the run and the close of the last candle in the run and apply the difference to each line. The result would look like</p>
  
  <pre><code>    date open close color  run  run_length
  00:01:00  100   102     g    1      2        # abs(100 - 102)
  00:02:00  102   104     g    2      4        # abs(100 - 104)
  00:03:00  104   106     g    3      6        # abs(100 - 106)
  00:04:00  106   105     r    1      1        # abs(106 - 105)
  00:05:00  105   101     r    2      5        # abs(106 - 101)
  00:06:00  101   102     g    1      1        # abs(101 - 102)
  00:06:00  102   103     g    2      2        # abs(101 - 103)
  </code></pre>
  
  <p>I have read two other posts that come close but don't quite get to the solution I'm looking for: </p>
  
  <p><a href="https://stackoverflow.com/questions/38797271/get-first-and-last-values-in-a-groupby/38797283">get first and last values in a groupby</a></p>
  
  <p><a href="https://stackoverflow.com/questions/48259470/pandas-number-of-consecutive-occurrences-in-previous-rows">Pandas number of consecutive occurrences in previous rows</a></p>
  
  <p>I'm using <code>df.groupby((df['color'] != df['color'].shift()).cumsum())</code> to group the rows by the color of the candle (this is how I calculated the color and the run count) and I can get the first and last values of the group using <code>.agg(['first', 'last']).stack()</code> but this doesn't allow me to apply the difference per line of the original dataframe.</p>