Back

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

I am trying to split a column into multiple columns based off comma/space seperation.

my dataframe currently looks like

    Item                                    Colors

0   ID-1                                   Red, Blue, Green

1   ID-2                                   Red, Blue

2   ID-3                                   Blue, Green

3   ID-4                                   Blue

4   ID-5                                   Red

I would like to transform the 'Colors' column into Red, Blue and Green like this:

    Item                                    Red  Blue  Green

0   ID-1                                    1     1      1

1   ID-2                                    1     1      0

2   ID-3                                    0     1      1

3   ID-4                                    0     1      0

4   ID-5                                    1     0      1

I really have no idea how to do this. Any help would be greatly appreciated.

1 Answer

0 votes
by (41.4k points)

Here, you can use get_dummies to get the intended output:

pd.concat([df,df.Colors.str.get_dummies(sep=', ')],1)

Out[450]: 

   Item          Colors          Blue  Green  Red

0  ID-1       Red,Blue,Green      1     1      1

1  ID-2          Red,Blue         1     0      1

2  ID-3        Blue,Green         1     1      0

3  ID-4           Blue            1     0      0

4  ID-5           Red             0     0      1

If you wish to learn more about how to use python for data science, then go through this data science python course by Intellipaat for more insights.

Browse Categories

...