简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM