Skip to content
Advertisement

pd.get_dummies() not converting categorical data to one hot encoded vectors when multiple features are used

I just got started on Kaggle and for my first project I was working on the Titanic dataset.

I ran the following codeblock

JavaScript

Although I’m getting the output as:

JavaScript

The Pclass, SibSp and Parch variables did not convert to one_hot encoded vectors though the Sex attribute did.

I didn’t understand why because when I try to run pd.get_dummes() function on the Pclass variable alone, the result it gives me is perfectly fine.

JavaScript

Although the names of the columns have been converted to “0”, “1” and “2” which of course is not fine actually…

But how can I fix the problem? I want all the features to be converted to one-hot encoded vectors.

Advertisement

Answer

Use OneHotEncoder from sklearn

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement