3

I have a dataset, df, where I would like to create unique ids for the values in the type column by placing numbers on the end.

Data

type    total   free  use
a       10      5     5
a       10      4     6
a       10      1     9
a       10      8     2
a       10      3     7
b       20      5     5
b       20      3     7
b       20      2     8
b       20      6     4
b       20      2     8

Desired

type    total   free  use
a       10      5     5
a1      10      4     6
a2      10      1     9
a3      10      8     2
a4      10      3     7
b       20      5     5
b1      20      3     7
b2      20      2     8
b3      20      6     4
b4      20      2     8

Doing

I was able to do this in R by doing, but unsure of how to do this in Python:

library(data.table)
setDT(DT)

DT[ , run_id := rleid(ID)]
DT[DT[ , .SD[1L], by = run_id][duplicated(ID), ID := paste0('list', .I)],
   on = 'run_id', ID := i.ID][]

I am researching this, any input is appreciated

1 Answer 1

5

You can use groupby.cumcount:

df['type'] += np.where(df['type'].duplicated(),
                       df.groupby('type').cumcount().astype(str), 
                       '')

Or similarly with loc update:

df.loc[df['type'].duplicated(), 'type'] += df.groupby('type').cumcount().astype(str)

Output:

  type  total  free  use
0    a     10     5    5
1   a1     10     4    6
2   a2     10     1    9
3   a3     10     8    2
4   a4     10     3    7
5    b     20     5    5
6   b1     20     3    7
7   b2     20     2    8
8   b3     20     6    4
9   b4     20     2    8
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Quang, so the groupby.cumcount() function is what creates the consecutive values? Could you tell me what is the += . Is this shorthand for df['type'] = df['type'] + np.where....

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.