0

I have a dataframe with a name column. I want to edit the string for those values that are repeated.

Example:

  name
J. Doe
J. Doe
J. Doe

Expected output

    name
  J. Doe
J. Doe 1
J. Doe 2
mozway
  • 194,879
  • 13
  • 39
  • 75
Omar B
  • 45
  • 7
  • please format the text in your question appropriately, either as formatted text like above or (better) as pandas dataframe as this enables direct copy pasting – mozway May 02 '22 at 10:59

1 Answers1

0

IIUC, you can use:

num = df.groupby('name').cumcount()
df.loc[num.ne(0), 'name'] += ' '+num.astype(str)

output:

       name
0    J. Doe
1  J. Doe 1
2  J. Doe 2

used input:

df = pd.DataFrame({'name': ['J. Doe', 'J. Doe', 'J. Doe']})
mozway
  • 194,879
  • 13
  • 39
  • 75