Split categorical column into multiple columns with values from another column

Turns out it’s quite simple if you know the right pandas methods.

There’s a simple way to split categorical column into multiple columns with panda’s get_dummies. That is one separate column for each category. Yes, all done automatically.

Now we have a bunch of columns with category name in the column names and 0’s and 1’s as the values. But then, we don’t want just 1’s, but rather actual values from another column.

The easiest way to do this is using Panda’s .mul()

dummies = pd.get_dummies(df['CategoryColumn']).mul(df.ActualValueColumn,0)

The more I dive into Pandas, the more I like it! There’s always the right function when you need it ­čÖé

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x