# Encoding categorical data with pre-determined dictionary

in case feature encoding, if I'd like to encode my values based on my pre-determined dictionary, how do I do that?

For instance, say, I've values as [Red, Green, and Blue] and I want to encode them as [-1,0,1] -1 for red, 0 for Green, 1 for Blue... I'll apply it to my feature. I believe I can do it by mapping, apply method, not sure. But is there any better way to do that?

Column     expectedEncoding
Red             -1
Red             -1
Blue             1
Green            0
Red             -1
Blue             1

$$$$

• What technology are you using? What library? Sep 10 '20 at 9:27
• sklearn, pandas, numpy etc. Sep 10 '20 at 9:32
• Then your best approach is just using df[col].map(mapping) where col is the name of the column to be encoded and mapping is a dictionary with the values Sep 10 '20 at 9:36
• Alright, I thought there might be another way for that. Well then, can I do it for different columns with different dictionaries at once? Sep 10 '20 at 10:29
• I'll be answering this in a post Sep 10 '20 at 11:17

Assuming you have a pandas DataFrame and one mapping per column, with all mappings stored in a 2-level dict where the keys of the first level correspond to the columns in the dataframe and the keys of the second level correspond to the categories:

{'fruit': {'banana': -1, 'apple': 1}, 'color': {'yellow': -1, 'red': 1}}


Then, you can do the following:

encoded_data = data.apply(lambda col: col.map(mappings[col.name]))


[EDIT] if have columns for which you don't have a mapping, you can do one of the following:

data.update(data[list(mappings)].apply(lambda col: col.map(mappings[col.name])))


or if you want it in a new dataframe (eg to keep the dataframe with the original values):

encoded_data = data.copy()
encoded_data.update(data[list(mappings)].apply(lambda col: col.map(mappings[col.name])))

• There is key error if the column name is not in the dict Sep 10 '20 at 11:47
• yes this is expected. You can filter the columns first if you want to avoid this Sep 10 '20 at 12:07
• Can you please also show me how to do that? Sep 10 '20 at 12:17
• Check the updated answer Sep 10 '20 at 12:24

You can use:

df.replace({'fruit': {'banana': -1, 'apple': 1}, 'color': {'yellow': -1, 'red': 1}},inplace=True)


given that 'fruit' and 'banana'` are columns in your data-frame.