I have a pandas dataframe that looks like this.
col1 col2 Col3 target
1 3 7 0
2 4 8 1
1 3 7 1
2 4 8 0
1 3 7 1
2 4 8 0
1 3 7 1
2 4 8 1
1 3 7 1
2 4 8 1
2 4 8 0
1 3 7 0
2 4 8 0
1 3 7 0
I would like to count the past identical target occurrences.
For example, if the current row target is 1 and the last 3 row target 1 as well, then I would like to have the value 3 in the count column.
More examples:
Current row target is 1; Last row target is 0; Count is 0.
Current row target is 1; Last row target is 1; Count is 1.
Current row target is 1; Last 2 row target is 1; Count is 2.
Current row target is 0; Last 2 row target is 0; Count is 2.
This is how the modified df looks like.
col1 col2 Col3 target count
1 3 7 0 0
2 4 8 1 0
1 3 7 1 1
2 4 8 0 0
1 3 7 1 0
2 4 8 0 0
1 3 7 1 0
2 4 8 1 1
1 3 7 1 2
2 4 8 1 3
2 4 8 0 0
1 3 7 0 1
2 4 8 0 2
1 3 7 0 3
Is there any easier way to do this in pandas?