Here is what we can read from the pandas map function documentation :
When arg is a dictionary, values in Series that are not in the dictionary (as keys) are converted to NaN. However, if the dictionary is a dict subclass that defines missing (i.e. provides a method for default values), then this default is used rather than NaN
So I tried to apply this note with this code :
import pandas as pd
class Missing_dict(dict) :
def __init__(self,*arg,**kw):
super(Missing_dict, self).__init__(*arg, **kw)
def __missing__(self, key) :
return key
md = Missing_dict({"a" : 0})
df = pd.DataFrame([{"key" : "a", "value" : 0}, {"key" : "b", "value" : 1}])
print (df)
print (df["key"].map(md))
But the result is not what I expect, in my case I want to map the data from one column, and if a value is not found in the dictionnary, then I would like this value as result (in other words, I would like the value if the key is in the dictionary, otherwise the key).
Where am I wrong ?