pandas map makes values NaN

Viewed 3650

I'm trying to map my values that i want to change. When i apply 'map' like this >> df[column].map(dictionary), the values that are not in the dictionary convert to NaN. I think the reason is that there are no matched values in the series, right? If so, nothing should be applied instead converting to NaN? How can i solve this problem using df.map() instead of df.replace()?

df1 = pd.Series(['a','b','c','d'])
df
0    a
1    b
2    c
3    d
dtype: object

mapping = {'a' : 0, 'b' : 1, 'c' : 2}
df1.map(mapping)
0    0.0
1    1.0
2    2.0
3    NaN
dtype: float64

or

df1 = pd.Series(['a','b','c','d'])
df
0    a
1    b
2    c
3    d
dtype: object

mapping = {'k' : 0, 'e' : 1, 'f' : 2}
df1.map(mapping)

0   NaN
1   NaN
2   NaN
3   NaN
dtype: float64
3 Answers

If you insist on map pass a callable instead

df.map(lambda x: mapping.get(x,x))

To change the default value, you could add a function (func, here):

mapping = {'k' : 0, 'e' : 1, 'f' : 2}
mapping.setdefault('Default', 'write watherver you want here')
def func(x, mapping):
    try:
        tmp=mapping[x]
        return(tmp)
    except:
        return('default value')
df1.map(lambda x: func(x, mapping))

This behavior is intended. Since mapping can not be applied the value is NaN. In order to use mapping you have to create a specific value that does not change your data (if you do multiplication that would be 1, if you do addition then 0) and add that value to your mapping.

Alternatively you could replace all NaN values after you have done the mapping with a neutral value like 0.0.

Either way is much more work then to simply use replace.

Related