Jamgreen
Jamgreen

Reputation: 11039

Elementwise operation on pandas series

I have a pandas Series x with values 1, 2 or 3.

I want it to have values monkey, gorilla, and tarzan depending on the values.

I guess I should do something like

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

x = values[x - 1]

but it doesn't work. I guess it's because it doesn't operate elementwisely.

Upvotes: 0

Views: 331

Answers (1)

jezrael
jezrael

Reputation: 862771

Use maping by dict with function map.

Sample:

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

Upvotes: 2

Related Questions