I have the following data with the last column as the desired output:
activity | teacher | group | students | the desired column |
---|---|---|---|---|
One | A | a | 3 | 0 |
One | B | b | 2 | 0 |
two | A | c | 7 | 0 |
One | D | a | 3 | 1 |
two | C | c | 7 | 1 |
when I have the same group with more than one teacher in the same activity, I want to return 0 for the first instance with the first teacher and 1 , 2, 3, .. for the following instances.I tried the following code
df.groupby(['activity','group').teacher.transform('count')
the output of this looks like:
activity | teacher | group | students | the output column |
---|---|---|---|---|
One | A | a | 3 | 0 |
One | B | b | 2 | 0 |
two | A | c | 7 | 0 |
One | A | a | 3 | 0 |
two | C | c | 7 | 0 |
thank you in advance for any suggestion.
So you just want to count the number of rows per group/activity
df.groupby(['group','activity']).cumcount()