Sep-20-2020, 05:02 AM
Hi,
I want to assign serial number but reset at repeating row
I use the below code, but I am not achieving the desired output.
I want to assign serial number but reset at repeating row
I use the below code, but I am not achieving the desired output.
import pandas as pd df = pd.DataFrame({'ID':['A1','A2','A1','A3','A4','A3','A4','A5'],\ 'sub_set':['A.0','A.0','A.0','A.0','A.0','A.0','A.0','A.0'],\ 'grp':['A','A','A','A','A','A','A','A'],\ 'rank':[1,2,1,1,2,3,9,6]}) df['C'] = df.groupby(['sub_set']).cumcount()+1I am getting below output:
ID grp rank sub_set C A1 A 1 A.0 1 A2 A 2 A.0 2 A1 A 1 A.0 3 A3 A 1 A.0 4 A4 A 2 A.0 5 A3 A 3 A.0 6 A4 A 9 A.0 7 A5 A 6 A.0 8But desired output:
ID grp rank sub_set C A1 A 1 A.0 1 A2 A 2 A.0 2 A1 A 1 A.0 1 A3 A 1 A.0 2 A4 A 2 A.0 3 A3 A 3 A.0 1 A4 A 9 A.0 2 A5 A 6 A.0 3