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 contains 0
, then increment the streak
value from the previous row by 0
. if it is not 0
, then start a new streak
, that is, put 0
into the streak
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…