Sum of next n rows in python
<p>I have a dataframe which is grouped at product store day_id level Say it looks like the below and I need to create a column with rolling sum </p>

<pre><code>prod store day_id visits 
111  123   1          2 
111  123   2          3
111  123   3          1
111  123   4          0
111  123   5          1
111  123   6          0
111  123   7          1
111  123   8          1
111  123   9          2
</code></pre>

<p>need to create a dataframe as below</p>

<pre><code>prod store day_id visits rolling_4_sum  cond
111  123   1          2  6              1
111  123   2          3  5              1
111  123   3          1  2              1
111  123   4          0  2              1
111  123   5          1  4              0
111  123   6          0  4              0
111  123   7          1  NA             0
111  123   8          1  NA             0
111  123   9          2  NA             0
</code></pre>

<p>i am looking for create a
 <strong>cond column</strong>: that recursively checks a condition , say if rolling_4_sum     is greater than 5 then make the next 4 rows as 1 else do nothing ,i.e. even if the condition is not met retain what was already filled before , do this check for each row until 7 th row.</p>

<p>How can i achieve this using python ? i am trying </p>

<pre><code>d1['rolling_4_sum'] = d1.groupby(['prod', 'store']).visits.rolling(4).sum() 
</code></pre>

<p>but getting an error.</p>