Today, I tried a data transformation that seemed so obvious: splitting the string values of a Pandas column on a delimiter and one-hot encode the resulting strings. However, it took me quite some time to figure out how to do it elegantly.
Here’s what I wanted to achieve. I had a DataFrame like this:
index | string_column |
1 | apple,pear,banana |
2 | apple |
3 | pear,apple |
I wanted to turn it into this:
Index | string_column | apple | pear | banana |
1 | apple,pear,banana | 1 | 1 | 1 |
2 | apple | 1 | 0 | 0 |
3 | pear,apple | 0 | 1 | 1 |
To break it down, this can be achieved by doing two transformations:
- Split string on a delimiter
- One-hot encode the resulting values
Although it looks seemingly easy, I had a hard time imagining how one goes from the intermediate state (columns with the first, second and third string after splitting them) to the final state.
Luckily, Pandas has an out-of-the-box method for achieving both transformations at once. That method is the get_dummies Series method, which differs a lot from Pandas’ general function with the same name.
By using the sep parameter, one can apply one-hot encoding to a single Series that has multiple values split by a delimiter:
df['string_column'].str.get_dummies(sep = ',')
Simple as that!
By the way, I didn’t necessarily come up with this solution myself. Although I’m grateful you’ve visited this blog post, you should know I get a lot from websites like StackOverflow and I have a lot of coding books. This one by Matt Harrison (on Pandas 1.x!) has been updated in 2020 and is an absolute primer on Pandas basics. If you want something broad, ranging from data wrangling to machine learning, try “Mastering Pandas” by Stefanie Molin.
Hey,
I always found this process of splitting Panda column extremely tricky. Your article helped a lot. Thanks man!
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.com/ro/register?ref=YY80CKRN
Your article helped me a lot, is there any more related content? Thanks! https://accounts.binance.com/uk-UA/register?ref=GJY4VW8W
Itís difficult to find experienced people for this topic, but you seem like you know what youíre talking about! Thanks
May I simply say what a comfort to discover somebody who genuinely knows what they are talking about over the internet. You actually understand how to bring a problem to light and make it important. More people ought to check this out and understand this side of the story. I cant believe you arent more popular because you surely possess the gift.
This is a great tip!
This is a great tip!