Split Column into Unknown Number of Columns by Delimiter Pandas

℡╲_俬逩灬. 提交于 2020-01-23 03:16:04

问题


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:


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


来源:https://stackoverflow.com/questions/50069693/split-column-into-unknown-number-of-columns-by-delimiter-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!