Reputation: 177
I am trying to replace duplicates with the dict below nth. I'm wondering how I am able to ignore certain values so they aren't replaced. For example if the name 'Jack' comes up don't change it to 'firstJack' just leave it as 'Jack'.
import pandas as pd
import numpy as np
nth = {
0: 'first',
1: 'second',
2: 'third',
3: 'fourth'}
data = {'Name':['Tom', 'Tom', 'Jack', 'Terry'], 'Age':[20, 21, 19, 18]}
df = pd.DataFrame(data)
df.Name = df.groupby('Name').cumcount().map(nth) + df.Name
print(df)
Upvotes: 1
Views: 53
Reputation: 402333
You can apply a condition on the groupby
output so you add no prefix if the row matches your condition.
to_ignore = ['Jack']
(np.where(df['Name'].isin(to_ignore), '', df.groupby('Name').cumcount().map(nth))
+ df['Name'])
0 firstTom
1 secondTom
2 Jack
3 firstTerry
Name: Name, dtype: object
Upvotes: 1