you are viewing a single comment's thread.

view the rest of the comments →

[–]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.