pollinei
pollinei

Reputation: 5

Creating a new column from another column + unique numeric index in pandas dataframe

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.

Upvotes: 0

Views: 67

Answers (1)

Erfan
Erfan

Reputation: 42916

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

Upvotes: 3

Related Questions