将整个整数数组转换为对象

时间:2018-08-01 14:24:23

标签: python arrays dictionary object

enter image description here

for i in predictions:     
    if i==0.0:
        i=str('Grade 1')        
    elif i==1.0:
        i=str('Grade 2')
    elif i==2.0:
        i=str('Grade 3')
    elif i==3.0:
        i=str('Grade 4')
    elif i==4.0:
        i=str('Grade 5')

数组中没有更改,未映射,请帮助

2 个答案:

答案 0 :(得分:0)

如果要映射标签,则df是数据框,而cols是01

labels = pd.Series({
    0: "Orange",
    2: "Blue",
    4: "Red",
})

df[1] = df[1].astype(np.int)
df.join(labels.to_frame("label"), on=1)

答案 1 :(得分:0)

使用rename_categories

df = pd.DataFrame({'A':[random.randint(0,4) for i in range(10)]})
   A
0  3
1  4
2  1
3  1
4  2
5  3
6  1
7  1
8  1
9  0


#Must convert to categorical first then use rename_categories
 df.A.astype('category')\
    .cat.rename_categories(['Apple','Orange','Banana','Peach','Cherry'])

0     Peach
1    Cherry
2    Orange
3    Orange
4    Banana
5     Peach
6    Orange
7    Orange
8    Orange
9     Apple