Skip to content Skip to sidebar Skip to footer

Pandas Change A Specific Column Value Of Duplicate Rows

Using the example here Drop all duplicate rows in Python Pandas Lets say I don't want to drop the duplicates but change the value of the data in one of the columns in the subset. S

Solution 1:

You could use cumcount and do something like

>>>c = df.groupby(["A","C"]).cumcount()>>>c = c.replace(0, '').astype(str)>>>df["A"] += c>>>df
      A  B  C
0   foo  0  A
1  foo1  1  A
2   foo  1  B
3   bar  1  A

This works because the cumcount gives us

>>>df.groupby(["A","C"]).cumcount()
0    0
1    1
2    0
3    0
dtype: int64

Post a Comment for "Pandas Change A Specific Column Value Of Duplicate Rows"