0 votes
1 view
in Data Science by (50.5k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...