4

I have a dictionary of lookup of values dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 3}

The result I want from numpy is what pandas.Series.map() returns after passing in the dictionary. Eg series.map(dictionary, na_action='ignore')

NOTE: This series.map() function is extremely fast which makes me believe there must be an equivalent in the numpy API rather than me implementing some solution involving numpy.where() and for looping through the dictionary keys.

SARose
  • 3,558
  • 5
  • 39
  • 49
  • For the general (non-dict) case, see [Most efficient way to map function over numpy array](https://stackoverflow.com/questions/35215161/most-efficient-way-to-map-function-over-numpy-array). – jpp Oct 04 '18 at 19:18

1 Answers1

4

Here's a NumPy one -

def map_series_by_dict(s, d):
    a = s.values
    v = np.array(list(d.values()))
    k = np.array(list(d.keys()))    
    sidx = k.argsort()
    out_ar = v[sidx[np.searchsorted(k,a,sorter=sidx)]]
    return pd.Series(out_ar, index=s.index)

Sample run -

In [143]: d
Out[143]: {'a': 1, 'b': 2, 'c': 3, 'd': 3}

In [144]: s
Out[144]: 
0    a
1    a
2    c
3    b
4    a
dtype: object

In [145]: map_series_by_dict(s, d)
Out[145]: 
0    1
1    1
2    3
3    2
4    1
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • If I understand correctly, does this factorise input values so that you don't have to call `dict.get` on each value individually? I assume that's the purpose of `sidx` + `np.searchsorted`. If that's the case, it's worth noting this is why it's faster than the generic `np.vectorize` with `dict.get`. – jpp Oct 04 '18 at 19:21
  • Also, possibly your answer should go here: [Translate every element in numpy array according to key](https://stackoverflow.com/questions/16992713/translate-every-element-in-numpy-array-according-to-key). Where it's likely to generate more views + upvotes :). – jpp Oct 04 '18 at 19:24
  • 2
    @jpp Well that extracting part to get values and keys off dict could be thought off as the setup overhead and should be vectorized thereafter. Good call on finding the more reaching Q&A. Would remind myself to add this searchsorted based on it soon, thanks! – Divakar Oct 04 '18 at 19:31