miro_muras
miro_muras

Reputation: 43

How can I rename duplicated values in Pandas Series?

I've used a custom function to find duplicates in series, which returns duplicated values.

def find_duplicates(self,data, key): 
    return result

Then, I've tried to use a nested function where I want these values to be renamed/duplicates replaced with _2,_3,_4...

def rename_duplicates(self,data=None,key=None):
    count = 1
    duplicates = self.find_duplicates(data,key)
    
    return data

The result is a list of duplicated values: [apple, apple, apple, banana, banana, orange, orange]

The goal is to rename each value by joining an integer for a sequence.[apple_2 apple_3, apple_4, banana_2, banana_3, orange_2, orange_3 ]

Upvotes: 0

Views: 248

Answers (1)

BENY
BENY

Reputation: 323236

Try with cumcount

df['new'] = df['yourcol'] + '_' +df.groupby('yourcol').cumcount().add(1).astype(str)

Upvotes: 1

Related Questions