Reputation: 5
Is there a way to do this without a loop?
I am thinking about something like this:
pd_series = pd.Series(["c", "d", "a", "b"])
list_map = ["a", "b", "c", "d"]
In [1]: pd_series.find_in(list_map)
Out [1]:
0 2
1 3
2 0
3 1
Thanks!
Upvotes: 0
Views: 92
Reputation: 323226
You can using np.vectorize
+get_loc
np.vectorize(pd.Index(pd_series).get_loc)(list_map)
Out[499]: array([2, 3, 0, 1])
Upvotes: 1
Reputation: 95873
You could use an actual map instead of a list, assuming the elements are unique:
>>> actual_map = dict(zip(list_map, range(len(list_map))))
>>> actual_map
{'d': 3, 'b': 1, 'a': 0, 'c': 2}
>>> pd_series.map(actual_map)
0 2
1 3
2 0
3 1
dtype: int64
Upvotes: 3