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
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
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']})