title: Calculate cumulative sum while value of another column stays the same
body: <p>For the following <code>df</code>, I want to calculate the cumulative sum of the column <code>Inst_Dist</code> and save as <code>Cumu_Dist</code> while the value of <code>WDir_Deg</code> stays the same. When the value in <code>WDir_Deg</code> changes, I need to restart the cumulative sum.</p>
  
  <p>Therefore,</p>
  
  <pre><code>index | WDir_Deg | Inst_Dist | Cumu_Dist
  0     | 289      | 20        | NaN
  1     | 285      | 17        | NaN
  2     | 285      | 19        | NaN
  3     | 287      | 19        | NaN
  4     | 289      | 10        | NaN
  </code></pre>
  
  <p>becomes</p>
  
  <pre><code>index | WDir_Deg | Inst_Dist | Cumu_Dist
  0     | 289      | 20        | 20
  1     | 285      | 17        | 17
  2     | 285      | 19        | 36
  3     | 287      | 19        | 19
  4     | 289      | 10        | 10
  </code></pre>
  
  <p>My non-idiomatic (extremely slow) Python code is given below. I'd really appreciate if someone can guide me on how to make the code faster and idiomatic.</p>
  
  <pre><code>prev_angle = -1
  curr_cumu_dist = 0
  for curr_ind in df.index:
      curr_angle = df.loc[curr_ind, 'WDir_Deg']
      if prev_angle == curr_angle:
          curr_cumu_dist += df.loc[curr_ind, 'Inst_Dist']
          df.loc[curr_ind, 'Cumu_Dist'] = curr_cumu_dist
      else:
          prev_angle = curr_angle
          curr_cumu_dist = df.loc[curr_ind, 'Inst_Dist']
          df.loc[curr_ind, 'Cumu_Dist'] = curr_cumu_dist
  </code></pre>