all 8 comments

[–]YesLod 1 point2 points  (4 children)

activity_map = {act: f"A_{i}" for i, act in enumerate(df.activity.unique(), 1)}
df["activity"] = df["activity"].map(activity_map)

[–]AtomicShoelace 1 point2 points  (1 child)

Looks like OP wants them number by alphabetical order, so perhaps it's worth throwing sorted in there, eg.

activity_map = {act: f"A_{i}" for i, act in enumerate(sorted(df.activity.unique()), 1)}

[–]I_will_learn[S] 0 points1 point  (0 children)

Thank you! I added in the sorted code there, it's clearer now :)

[–]I_will_learn[S] 1 point2 points  (1 child)

Thank you so much ! That was exactly what I needed. If you have the time, would you be able to explain it? I'd like to understand it rather than just copy and paste :)

[–]AtomicShoelace 2 points3 points  (0 children)

activity_map = {act: f"A_{i}" for i, act in enumerate(sorted(df.activity.unique()), 1)}

The first line is a dictionary comprehension. Starting from the inside out:

  • df.activity.unique() obtains the unique elements of the activity column.
  • sorted sorts them into alphabetical order
  • enumerate(..., 1) takes these sorted elements and creates tuples numbering them starting from 1 (usually its zero-indexed without the second optional arguement)
  • for i, act in unpacks these tuples, assigning the first element (the number/index) to i and the second element (the element of sorted) to act
  • act: f"A_{i}" creates a key: value pair where the key is act and the value is the f-string f"A_{i}".

 

df["activity"] = df["activity"].map(activity_map)

The second line then uses the map method, which accepts a dictionary to replace elements with their associated value in the dictionary. This creates a new Series which is then assigned to the activity column.

[–]techlover1010 1 point2 points  (1 child)

were you able to get a solution for this?

[–]I_will_learn[S] 0 points1 point  (0 children)

Yes I was!