按预定义规则将字符串列编码为数值

时间:2017-06-01 07:47:26

标签: python pandas encoding machine-learning scikit-learn

我有以下DataFrame:

   voting_result project   scene
    Bad          ccus      345943
    Good         ccus      311129
    Bad          ccus      309082
    Bad          ccus      331613
    Good         ccus      331615 
    Not Sure     ccus      331616   

通过遵循规则Pandas} encode{'Bad':0, 'Good':1, 'Not Sure':2 voting_result列到整数的方式是什么

voting_result    project    scene
        0         ccus      345943
        1         ccus      311129
        0         ccus      309082
        0         ccus      331613
        1         ccus      331615 
        2         ccus      331616

由于

1 个答案:

答案 0 :(得分:5)

dictionary使用map

d = {'Bad':0, 'Good':1, 'Not Sure':2}
df['voting_result'] = df['voting_result'].map(d)
print (df)
   voting_result project   scene
0              0    ccus  345943
1              1    ccus  311129
2              0    ccus  309082
3              0    ccus  331613
4              1    ccus  331615
5              2    ccus  331616