问题
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