Running get_dummies on several DataFrame columns?

Emre picture Emre · Jun 8, 2014 · Viewed 82.1k times · Source

How can one idiomatically run a function like get_dummies, which expects a single column and returns several, on multiple DataFrame columns?

Answer

mxdbld picture mxdbld · Dec 4, 2016

With pandas 0.19, you can do that in a single line :

pd.get_dummies(data=df, columns=['A', 'B'])

Columns specifies where to do the One Hot Encoding.

>>> df
   A  B  C
0  a  c  1
1  b  c  2
2  a  b  3

>>> pd.get_dummies(data=df, columns=['A', 'B'])
   C  A_a  A_b  B_b  B_c
0  1  1.0  0.0  0.0  1.0
1  2  0.0  1.0  0.0  1.0
2  3  1.0  0.0  1.0  0.0