I have a column in a data frame which looks like –

Key |
---|

A |

B |

C |

A |

A |

I want to transform this so that each key has a suffix “_” + “order of occurrence if value is repeated” i.e. to look like –

Key |
---|

A_1 |

B |

C |

A_2 |

A_3 |

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.

## Answer

Let us try with `cumcount`

then `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