pandas中是非常容易实现的,转换成Categories对象即可。
c = ['A','A','A','B','B','C','C','C','C','d','D','Good']
# 将字符标签或者类别数字化
category = pd.Categorical(c)
print(category)
print('---')
# 查看数字化后的标签
print(category.codes)
[A, A, A, B, B, ..., C, C, d, D, Good]
Length: 12
Categories (6, object): [A, B, C, D, Good, d]
---
[0 0 0 1 1 2 2 2 2 5 3 4]