最佳答案
您可以尝试以下方法:
# create a mapping for artist to a number
maps = {ar:en for en, ar in enumerate(df['artist'].unique())}
df['artist_code'] = df['artist'].map(maps)
artist artist_code
0 a 0
1 a 0
2 a 0
3 b 1
4 c 2
Sample Data
df = pd.DataFrame({'artist':['a','a','a','b','c']})
关于python - 基于字符串分类变量生成数值分类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59957779/