0

I have a dataframe in which several rows of a column have the same value:

   unique_code      0
0   p01_PAR_1  zertara
1   p01_PAR_1    atera
2   p01_PAR_1       da
3   p01_MOT_1       ez
4   p01_MOT_1    dakit

I would like to redo that column or create a new one with one unique value, by adding a numeric index after the value, so it would result on something like this:

   unique_code       0
0   p01_PAR_1_1  zertara
1   p01_PAR_1_2    atera
2   p01_PAR_1_3       da
3   p01_MOT_1_1       ez
4   p01_MOT_1_2    dakit

This cannot be done by adding the row index to each row, since they have different and unrelated values.

1 Answer 1

2

Use GroupBy.cumcount then add it as a string:

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

   unique_code        0
0  p01_PAR_1_1  zertara
1  p01_PAR_1_2    atera
2  p01_PAR_1_3       da
3  p01_MOT_1_1       ez
4  p01_MOT_1_2    dakit
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.