blueice
blueice

Reputation: 134

How to edit the realization process of OneHotEncoder in pandas?

I want to use a function "pandas.get_dummies()" to convert a DataFrame "Type: [N,N,N,Y,Y,N,Y……]" to be "Type: [0,0,0,1,1,0,1……]"

import pandas as pd
# Define a DataFrame "df"
df = pd.get_dummies(df)

But the function always convert "N" to be "1" and "Y" to be "0". So the result is "Type: [1,1,1,0,0,1,0……]". That's not what I want ,because it will bring some trouble to my calculation.

What should I do to change it?

Upvotes: 1

Views: 33

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

You can use 1-pd.get_dummies(df) to map 1 to 0 and vice versa, like:

>>> df
   0
0  N
1  N
2  N
3  Y
4  Y
5  N
6  Y
>>> 1 - pd.get_dummies(df)
   0_N  0_Y
0    0    1
1    0    1
2    0    1
3    1    0
4    1    0
5    0    1
6    1    0

or if you convert this to booleans, you can use the boolean negation, like:

>>> ~pd.get_dummies(df, dtype=bool)
     0_N    0_Y
0  False   True
1  False   True
2  False   True
3   True  False
4   True  False
5  False   True
6   True  False

Upvotes: 1

Related Questions