1
$\begingroup$

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

```
$\endgroup$
5
  • $\begingroup$ What technology are you using? What library? $\endgroup$
    – qmeeus
    Commented Sep 10, 2020 at 9:27
  • $\begingroup$ sklearn, pandas, numpy etc. $\endgroup$
    – quilliam
    Commented Sep 10, 2020 at 9:32
  • $\begingroup$ 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 $\endgroup$
    – qmeeus
    Commented Sep 10, 2020 at 9:36
  • $\begingroup$ Alright, I thought there might be another way for that. Well then, can I do it for different columns with different dictionaries at once? $\endgroup$
    – quilliam
    Commented Sep 10, 2020 at 10:29
  • $\begingroup$ I'll be answering this in a post $\endgroup$
    – qmeeus
    Commented Sep 10, 2020 at 11:17

2 Answers 2

1
$\begingroup$

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])))
$\endgroup$
4
  • $\begingroup$ There is key error if the column name is not in the dict $\endgroup$
    – quilliam
    Commented Sep 10, 2020 at 11:47
  • $\begingroup$ yes this is expected. You can filter the columns first if you want to avoid this $\endgroup$
    – qmeeus
    Commented Sep 10, 2020 at 12:07
  • $\begingroup$ Can you please also show me how to do that? $\endgroup$
    – quilliam
    Commented Sep 10, 2020 at 12:17
  • 1
    $\begingroup$ Check the updated answer $\endgroup$
    – qmeeus
    Commented Sep 10, 2020 at 12:24
1
$\begingroup$

You can use:

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

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

$\endgroup$
1
  • $\begingroup$ This .replace(dic) seems to be shorter than .apply(lambda col: col.map(mappings[col.name])) $\endgroup$
    – Partha D.
    Commented Feb 2, 2023 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.