Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify which column to remove in get_dummies in pandas

I have a DataFrame column with 3 values - Bart, Peg, Human. I need to one-hot encode them such that Bart and Peg stay as columns and human is represented as 0 0.

Xi | Architecture
0  | Bart
1  | Bart
2  | Peg
3  | Human
4  | Human
5  | Peg
..
.

I want to one-hot encode them so that Human is represented as 0 0:

Xi |Bart| Peg
0  | 1  | 0
1  | 1  | 0
2  | 0  | 1
3  | 0  | 0
4  | 0  | 0
5  | 0  | 1

But when I do :

pd.get_dummies(df['Architecture'], drop_first = True)

it removes "Bart" and keeps the other 2. Is there a way to specify which column to remove?

like image 468
Kiera.K Avatar asked Sep 02 '25 16:09

Kiera.K


1 Answers

You could mask it:

df = df[['Xi']].join(pd.get_dummies(df['Architecture'].mask(df['Architecture']=='Human')))

Output:

   Xi  Bart  Peg
0   0     1    0
1   1     1    0
2   2     0    1
3   3     0    0
4   4     0    0
5   5     0    1

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!