I have a pandas dataframe as follows:
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
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.
I want to detect streaks of zeros for this stat per loser.
The expected result should look like this:
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
In pseudocode the algorithm should work like this:
.groupby
loser
column.- then iterate over each row of each
loser
group - in each row, look at the
stat
column: if it contains0
, then increment thestreak
value from the previous row by0
. if it is not0
, then start a newstreak
, that is, put0
into thestreak
column.
So the .groupby
is clear. But then I would need some sort of .apply
where I can look at the previous row? this is where I am stuck.