title: Update rows of Pandas column using previous rows of same column
body: <p>I have a Pandas data frame with the following columns:</p>
  
  <blockquote>
    <p>blocked,   rolling_mean,   cumulative_i</p>
  </blockquote>
  
  <p>I max trying to create a new column where:</p>
  
  <p><em>c_(i) = max(0, blocked_i - (rolling_mean_i + k) + c_(i-1)) -- where k = 2</em></p>
  
  <p>My current approach is:</p>
  
  <pre><code>    for i in range(df.shape[0]):
          if i &gt; 1:
              df.ix[i, 'cumulative_i'] = max(0, df['blocked'].iloc[i] -(df['rolling_mean'].iloc[i] + k) + df['cumulative_i'].iloc[i - 1])
  </code></pre>
  
  <p>Is there a more pythonic way of doing this?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I tried doing the following.</p>
  
  <pre><code>    df['cumulative_i'] = np.maximum(0, df['blocked'] - (df['rolling_mean'] + k) + df['cumulative_i'].shift())
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Cuakh.png" alt="Sample vectorised output and expected output"></p>
  
  <p>On the third row under vectorised_output. The value 153.48 is what one would get if we don't add the previous value 213.72 (i.e. 367.2 - 213.72 = 153.48).</p>
  
  <p>This is the output I would expect if I were only doing</p>
  
  <pre><code>df['cumulative_i'] = np.maximum(0, df['blocked'] - (df['rolling_mean'] + k))   
  </code></pre>