Replace category strings with integer codes. Assign each unique category an integer starting from 0. By hand, build a sorted-unique list, create a mapping dict, then look up each label. With pandas, astype('category').cat.codes produces the same codes because pandas assigns category codes in sorted alphabetical order.

By hand

Build unique = sorted(set(labels)) — the same alphabetical order pandas uses — then enumerate it to create mapping. The first loop shows the dict filling in with bird→0, cat→1, dog→2. The second loop applies the mapping to produce the code list.

naive.py
Replay: real traced execution (multi-file project)
labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']
unique = sorted(set(labels))
mapping = {}
for i, cat in enumerate(unique):
    mapping[cat] = i
result = []
for s in labels:
    result.append(mapping[s])
print('RESULT:', result)
  1. labels ← ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']

    1labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']2unique = sorted(set(labels))
    values this step['cat', 'dog', 'bird', 'dog', 'cat', 'bird']labels
  2. unique ← ['bird', 'cat', 'dog']

    1labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']2unique = sorted(set(labels))3mapping = {}
    values this step['bird', 'cat', 'dog']unique
  3. mapping ← {}

    2unique = sorted(set(labels))3mapping = {}4for i, cat in enumerate(unique):
    values this step{}mapping
  4. cat ← 'bird', i ← 0

    3mapping = {}4for i, cat in enumerate(unique):5    mapping[cat] = i
    values this step'bird'cat0i
  5. mapping ← {'bird': 0}

    4for i, cat in enumerate(unique):5    mapping[cat] = i6result = []
    values this step{} {'bird': 0}mapping
  6. cat ← 'cat', i ← 1

    3mapping = {}4for i, cat in enumerate(unique):5    mapping[cat] = i
    values this step'bird' 'cat'cat0 1i
  7. mapping ← {'bird': 0, 'cat': 1}

    4for i, cat in enumerate(unique):5    mapping[cat] = i6result = []
    values this step{'bird': 0} {'bird': 0, 'cat': 1}mapping
  8. cat ← 'dog', i ← 2

    3mapping = {}4for i, cat in enumerate(unique):5    mapping[cat] = i
    values this step'cat' 'dog'cat1 2i
  9. mapping ← {'bird': 0, 'cat': 1, 'dog': 2}

    4for i, cat in enumerate(unique):5    mapping[cat] = i6result = []
    values this step{'bird': 0, 'cat': 1} {'bird': 0, 'cat': 1, 'dog': 2}mapping
  10. for i, cat in enumerate(unique):

    3mapping = {}4for i, cat in enumerate(unique):5    mapping[cat] = i
  11. result ← []

    5    mapping[cat] = i6result = []7for s in labels:
    values this step[]result
  12. s ← 'cat'

    6result = []7for s in labels:8    result.append(mapping[s])
    values this step'cat's
  13. result ← [1]

    7for s in labels:8    result.append(mapping[s])9print('RESULT:', result)
    values this step[] [1]result
  14. s ← 'dog'

    6result = []7for s in labels:8    result.append(mapping[s])
    values this step'cat' 'dog's
  15. result ← [1, 2]

    7for s in labels:8    result.append(mapping[s])9print('RESULT:', result)
    values this step[1] [1, 2]result
  16. s ← 'bird'

    6result = []7for s in labels:8    result.append(mapping[s])
    values this step'dog' 'bird's
  17. result ← [1, 2, 0]

    7for s in labels:8    result.append(mapping[s])9print('RESULT:', result)
    values this step[1, 2] [1, 2, 0]result
  18. s ← 'dog'

    6result = []7for s in labels:8    result.append(mapping[s])
    values this step'bird' 'dog's
  19. result ← [1, 2, 0, 2]

    7for s in labels:8    result.append(mapping[s])9print('RESULT:', result)
    values this step[1, 2, 0] [1, 2, 0, 2]result
  20. s ← 'cat'

    6result = []7for s in labels:8    result.append(mapping[s])
    values this step'dog' 'cat's
  21. result ← [1, 2, 0, 2, 1]

    7for s in labels:8    result.append(mapping[s])9print('RESULT:', result)
    values this step[1, 2, 0, 2] [1, 2, 0, 2, 1]result
  22. s ← 'bird'

    6result = []7for s in labels:8    result.append(mapping[s])
    values this step'cat' 'bird's
  23. result ← [1, 2, 0, 2, 1, 0]

    7for s in labels:8    result.append(mapping[s])9print('RESULT:', result)
    values this step[1, 2, 0, 2, 1] [1, 2, 0, 2, 1, 0]result
  24. for s in labels:

    6result = []7for s in labels:8    result.append(mapping[s])
  25. stdout ← RESULT: [1, 2, 0, 2, 1, 0]

    8    result.append(mapping[s])9print('RESULT:', result)
    values this stepRESULT: [1, 2, 0, 2, 1, 0]stdout

With pandas

astype('category') converts the column to the pandas Categorical dtype and sets the category list in sorted order. .cat.codes returns the integer code for each element. The snapshot shows categories: ['bird', 'cat', 'dog'] so the code→label correspondence is immediately visible.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

labels = ['cat', 'dog', 'bird', 'dog', 'cat', 'bird']
df = pd.DataFrame({'c': labels})
s = df['c'].astype('category')
codes = s.cat.codes
result = codes.tolist()
print('categories:', s.cat.categories.tolist())
print('index:', codes.index.tolist())
print('dtype:', codes.dtype)
print('RESULT:', result)
categories: ['bird', 'cat', 'dog']
index: [0, 1, 2, 3, 4, 5]
dtype: int8
RESULT: [1, 2, 0, 2, 1, 0]

Implementation notes

  • pandas assigns category codes in sorted order, not first-seen order. The naive half uses sorted(set(labels)) to match — using insertion order (a plain dict walk) would produce different codes and break parity.
  • cat.codes dtype is int8; codes start at 0 for the first sorted category.
  • For explicit control over the code assignment (e.g. keeping a specific category as code 0), build a manual dict and use .map(dict) instead.
  • Cross-reference: encode-labels (ml-basics chapter) for one-hot and ordinal encoding alternatives.