I am trying to add a new column in panda dataframe where a sequence number will be filled out by each group.
In below, you can see the sample new column.
Here, when TYPID is zero it has 5 entry and when this is belong to same SID,SEMID,TYP,TYPID group then the NEW column is filled up with a sequence number (1, 2, 3, 4 - which has to maintain TID sequence)
ind | SID | SEMID | TID | TYP | TYPID | NEW |
---|---|---|---|---|---|---|
0 | 1 | 5 | 451 | A | 0 | 2 |
1 | 1 | 5 | 450 | A | 0 | 1 |
2 | 1 | 5 | 452 | A | 0 | 3 |
3 | 1 | 5 | 455 | P | 1 | 1 |
4 | 1 | 5 | 456 | D | 2 | 1 |
5 | 1 | 5 | 457 | D | 2 | 2 |
6 | 1 | 5 | 458 | R | 3 | 1 |
7 | 1 | 5 | 459 | A | 0 | 4 |
8 | 2 | 5 | 460 | A | 0 | 1 |
How can I achieve this in python? I am totally new to Python. It would be helpful if any expert help on this.
Thank you in advance.