JavaScript
x
4
1
df = pd.DataFrame({'A': ['x', 'y', 'x'], 'B': ['z', 'u', 'z'],
2
'C': ['1', '2', '3'],
3
'D':['j', 'l', 'j']})
4
I just want Column A and D to get dummies not for Column B. If I used pd.get_dummies(df)
, all columns turned into dummies.
I want the final result containing all of columns , which means column C and column B exit,like 'A_x','A_y','B','C','D_j','D_l'
.
Advertisement
Answer
It can be done without concatenation, using get_dummies() with required parameters
JavaScript
1
7
1
In [294]: pd.get_dummies(df, prefix=['A', 'D'], columns=['A', 'D'])
2
Out[294]:
3
B C A_x A_y D_j D_l
4
0 z 1 1.0 0.0 1.0 0.0
5
1 u 2 0.0 1.0 0.0 1.0
6
2 z 3 1.0 0.0 1.0 0.0
7