fred.schwartz
fred.schwartz

Reputation: 2155

Pandas Dataframe rename duplicate values in Row

I have a dataframe

COL1   COL2   COL3
 Red    Blue   Green
 Red    Yellow  Blue
 Blue   Red     Blue

I want to rename value in the dataframe if they appear 2x (or more) in a row

So the expected output is

COL1   COL2   COL3
 Red    Blue   Green
 Red    Yellow  Blue
 Blue   Red     2Blue

Upvotes: 2

Views: 1965

Answers (1)

anky
anky

Reputation: 75080

We can use a custom function here which will check if values are duplicated in a row and add an incremental counter to each of them after using series.mask:

def myf(x):
    counter = x.groupby(x).cumcount().add(1).astype(str)
    return x.mask(x.duplicated(),x.radd(counter))

print(df.apply(myf,axis=1))
#or df.T.apply(myf).T

   COL1    COL2   COL3
0   Red    Blue  Green
1   Red  Yellow   Blue
2  Blue     Red  2Blue

Upvotes: 3

Related Questions