finding streaks in pandas dataframe : <p>I have a pandas dataframe as follows:</p>

<pre><code>time    winner  loser   stat
1       A       B       0
2       C       B       0
3       D       B       1
4       E       B       0
5       F       A       0
6       G       A       0
7       H       A       0
8       I       A       1
</code></pre>

<p>each row is a match result. the first column is the time of the match, second and third column contain winner/loser and the fourth column is one stat from the match.</p>

<p>I want to detect streaks of zeros for this stat per loser.</p>

<p>The expected result should look like this:</p>

<pre><code>time    winner  loser   stat    streak
1       A       B       0       1
2       C       B       0       2
3       D       B       1       0
4       E       B       0       1
5       F       A       0       1
6       G       A       0       2
7       H       A       0       3
8       I       A       1       0
</code></pre>

<p>In pseudocode the algorithm should work like this:</p>

<ul>
<li><code>.groupby</code> <code>loser</code> column. </li>
<li>then iterate over each row of each <code>loser</code> group</li>
<li>in each row, look at the <code>stat</code> column: if it contains <code>0</code>, then increment the <code>streak</code> value from the previous row by <code>0</code>. if it is not <code>0</code>, then start a new <code>streak</code>, that is, put <code>0</code> into the <code>streak</code> column.</li>
</ul>

<p>So the <code>.groupby</code> is clear. But then I would need some sort of <code>.apply</code> where I can look at the previous row? this is where I am stuck.</p>
