0

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.

Jamgreen
  • 10,329
  • 29
  • 113
  • 224

1 Answers1

2

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252