python小白一枚,做实验过程中需要将字符串数据标签数字化,所以从网上搜索了一下方法,用pandas来实现:
import pandas as pd
c = ['A','A','A','B','B','C','C','C','C']
category = pd.Categorical(c)
print category.labels
但是报了以下错误:
‘Categorical’ object has no attribute ‘labels’
原因是padas的Categorical的对象labels和levels属性已经改为codes和categories,所以将代码修改如下:
import pandas as pd
c = ['A','A','A','B','B','C','C','C','C']
category = pd.Categorical(c)
print category.codes
即可获得结果。
认真学python,加油!