Reputation: 11793
I have an example dataframe like below:
df = pd.DataFrame({'name': ['red', 'orange', 'blue'],
'value': [22,44,66]})
name value
0 red 22
1 orange 44
2 blue 66
I need to replace red
with 1
and blue
with 2
, leaving orange
as it is, in the column name
.
I use map function like below:
df.name.map({'red': 0, 'blue': 1})
I got result like:
name value
0 0.0 22
1 NaN 44
2 1.0 66
In the column, orange
becomes NaN
What is the best way to replace the values?
I do not want to use df.name.str.replace(..)
many times to replace many values. In my real data, there are 10 values to replace in a column.
Thanks.
Upvotes: 1
Views: 5979
Reputation: 22493
You can use Series.replace
:
print (df["name"].replace({'red': 0, 'blue': 1}))
0 0
1 orange
2 1
Name: name, dtype: object
Upvotes: 5
Reputation: 869
One way can be creating your custom dict
and overiding __missing__
import pandas as pd
df = pd.DataFrame({'name': ['red', 'orange', 'blue'],
'value': [22,44,66]})
class Foo(dict):
def __missing__(self, key):
return key
df.name.map(Foo({'red': 0, 'blue': 1}))
From the documentation Series.map :
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.
Upvotes: 2
Reputation: 323226
Fix your code with fillna
df.name = df.name.map({'red': 0, 'blue': 1}).fillna(df.name)
0 0
1 orange
2 1
Name: name, dtype: object
Upvotes: 2