1

I have a dataframe containing two columns:

val1  |  val2
-------------
1        'dog'
2        'cat'
3        'pig'
4        'crab'

and a dictionary

my_dict = {'dog' : 'Hund', 'pig' : 'Schwein'}

I want to apply the dictionary to val2 just where the values exist in my_dict, otherwise just leave the original ones:

val1  |  val2
-------------
1        'Hund'
2        'cat'
3        'Schwein'
4        'crab'

I found similar answers to this question, but in all cases the elements that don't exist in the dict are sect to NaN.

Qubix
  • 4,161
  • 7
  • 36
  • 73
  • 1
    https://stackoverflow.com/questions/20250771/remap-values-in-pandas-column-with-a-dict – BENY Apr 02 '20 at 21:39
  • Specifically see the Non-Exhaustive Mapping in JohnE's answer: https://stackoverflow.com/a/41678874/4333359 – ALollz Apr 02 '20 at 21:40

0 Answers0