Elementwise operation on pandas series
<p>I have a pandas Series <code>x</code> with values <code>1</code>, <code>2</code> or <code>3</code>.</p>

<p>I want it to have values <code>monkey</code>, <code>gorilla</code>, and <code>tarzan</code> depending on the values.</p>

<p>I guess I should do something like</p>

<pre><code>values = ['monkey', 'gorilla', 'tarzan']

x = values[x - 1]
</code></pre>

<p>but it doesn't work. I guess it's because it doesn't operate elementwisely.</p>

<p>Use maping by <code>dict</code> with function <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html" rel="nofollow"><code>map</code></a>.</p>

<p>Sample:</p>

<pre><code>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
</code></pre>

python pandas sklearn-pandas