I have a dataframe as below:
A B code cumul_sum
group1 group1_1 A 1
group1 group1_1 A 2
group1 group1_1 B 1
group1 group1_1 A 1
group1 group1_1 A 2
group1 group1_1 A 3
group2 group2_1 A 1
group2 group2_1 A 2
group2 group2_1 A 3
Please assume column A and B are belonging to same category. I wanted to group and the expected result are as follows
A B code cumul_sum **groupcat**
group1 group1_1 A 1 group1
group1 group1_1 A 2 group1
group1 group1_1 B 1 group2
group1 group1_1 A 1 group3
group1 group1_1 A 2 group3
group1 group1_1 A 3 group3
group2 group2_1 A 1 group1
group2 group2_1 A 2 group1
group2 group2_1 A 3 group1
Since there is a code B that occurred in the middle, group1 is not assigned to the third row and then the next value is a fresh categorical value.
EDIT: To also reset the group-cat for group A and B.
Please advice