I have a column in a data frame which looks like –
I want to transform this so that each key has a suffix “_” + “order of occurrence if value is repeated” i.e. to look like –
Reading related threads I understand it would have to be a play on groupby and cumcount but cant seem to get the final solution. Any help would be appreciated.
Let us try with
mask with total
count for each group
g = df.groupby('Key') df['Key'] += g.cumcount().add(1).astype(str).radd('_').mask(g['Key'].transform('count')==1,'') df Key 0 A_1 1 B 2 C 3 A_2 4 A_3