Pandas: Get Dummies
You can try :
df = pd.get_dummies(df, columns=['type'])
Consider I have the following dataframe:
Survived Pclass Sex Age Fare
0 0 3 male 22.0 7.2500
1 1 1 female 38.0 71.2833
2 1 3 female 26.0 7.9250
3 1 1 female 35.0 53.1000
4 0 3 male 35.0 8.0500
There are two ways to implement get_dummies:
Method 1:
one_hot = pd.get_dummies(dataset, columns = ['Sex'])
This will return:
Survived Pclass Age Fare Sex_female Sex_male
0 0 3 22 7.2500 0 1
1 1 1 38 71.2833 1 0
2 1 3 26 7.9250 1 0
3 1 1 35 53.1000 1 0
4 0 3 35 8.0500 0 1
Method 2:
one_hot = pd.get_dummies(dataset['Sex'])
This will return:
female male
0 0 1
1 1 0
2 1 0
3 1 0
4 0 1