0

I have a dictionary, and want to create a new column which references this dict:

df['new'] = df['new'].apply(lambda x: dict.get(x))

.apply is notorious for being slow, is there a way of doing this operation and still taking advantage of numpy vectorization speed?

Jay
  • 31
  • 2

1 Answers1

1

Use Series.map

map accepts a dict or a Series. Values that are not found in the dict are converted to NaN, unless the dict has a default value (e.g. defaultdict):

df['new'] = df['new'].map(my_dict)
ansev
  • 30,322
  • 5
  • 17
  • 31