Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

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

1 Answer

0 votes
by (108k points)

You can simply perform that in a single line With pandas 0.19:

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

Columns specify 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

Browse Categories

...