2016-09-26 88 views
0

我有一個熊貓系列x與價值1,23熊貓系列的元素操作

我希望它具有值monkey,gorillatarzan,具體取決於值。

我想我應該這樣做

values = ['monkey', 'gorilla', 'tarzan'] 

x = values[x - 1] 

,但它不工作。我想這是因爲它不能單獨運作。

回答

2

使用由dict與功能map的地圖。

樣品:

s = pd.Series([1,2,3]) 
print (s) 
0 1 
1 2 
2 3 
dtype: int64 

d = {1:'monkey',2:'gorilla',3:'tarzan'} 
print (s.map(d)) 
0  monkey 
1 gorilla 
2  tarzan 
dtype: object