Reputation: 87
Sample dataset:
id count
0 3 NaN
1 3 cat
2 3 human
3 3 bird
4 3 NaN
5 8 pegion
6 8 NaN
7 8 NaN
8 8 rat
9 8 NaN
10 8 human
11 8 monkey
Loop through each row. In count
column, value of NaN
and human
is 0. All other strings value is 1. Then sum with previous row value until id
contains same value. (continue recursively)
The output I want:
id count
0 3 0
1 3 1
2 3 1
3 3 2
4 3 2
5 8 1
6 8 1
7 8 1
8 8 2
9 8 2
10 8 2
11 8 3
Upvotes: 0
Views: 56
Reputation: 11395
This can be done in 3 simple steps:
human
and non-NaN
id
>>> (df['count'].ne('human') & df['count'].notna()).groupby(df['id']).cumsum()
0 0
1 1
2 1
3 2
4 2
5 1
6 1
7 1
8 2
9 2
10 2
11 3
Upvotes: 1