Split Column into Unknown Number of Columns by Del

2020-07-26 11:03发布

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条回答
神经病院院长
2楼-- · 2020-07-26 11:59

You can using get_dummies

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
查看更多
登录 后发表回答