Converting a string to dummy encoded variables Here's the data

PlayerID, Characters, Win or Lose


I can make it look like this

8PYPY0LLQ,valkyrie5 ,  chr_witch4 ,  hog_rider5 ,  zapMachine1 ,  mega_minion3 ,  baby_dragon2 ,  bomber7 ,  skeleton_horde1, 0


Or like this

2GRG822L9,"barbarians8, valkyrie5, chr_balloon3, fire_spirits8, minion8, firespirit_hut6, rage4, skeleton_horde3,",1


The second column is an 8 character combination from 70+ n characters.

I need to encode the variables to be dummy variables, so each character gets its own column. Is there a way to do this in python/R? I'm assuming you have to leave the second column as a string rather than outputting a csv file that looks like this.

2GRG822L9,barbarians8, valkyrie5, chr_balloon3, fire_spirits8, minion8, firespirit_hut6, rage4, skeleton_horde3,1
8PYPY0LLQ,valkyrie5 ,  chr_witch4 ,  hog_rider5 ,  zapMachine1 ,  mega_minion3 ,  baby_dragon2 ,  bomber7 ,  skeleton_horde1,0


It should probably look like this before dummy encoding (I can get rid of commas within the string)

2GRG822L9,"barbarians8, valkyrie5, chr_balloon3, fire_spirits8, minion8, firespirit_hut6, rage4, skeleton_horde3,",1
8PYPY0LLQ,"valkyrie5 ,  chr_witch4 ,  hog_rider5 ,  zapMachine1 ,  mega_minion3 ,  baby_dragon2 ,  bomber7 ,  skeleton_horde1,",0
It is simple with python:

from pandas import DataFrame
data = [('2GRG822L9',"barbarians8,valkyrie5,chr_balloon3,fire_spirits8,minion8,firespirit_hut6,rage4,skeleton_horde3",1), ('8PYPY0LLQ',"valkyrie5,chr_witch4,hog_rider5,zapMachine1,mega_minion3,baby_dragon2,bomber7,skeleton_horde1",0)]
df = DataFrame.from_records(data,columns=('PlayerID', 'Characters', 'Result'))
df = df.drop('Characters', 1).join(df.Characters.str.get_dummies(','))


The result:

    PlayerID  Result  baby_dragon2  barbarians8  bomber7  chr_balloon3  \
0  2GRG822L9       1             0            1        0             1   
1  8PYPY0LLQ       0             1            0        1             0   

   chr_witch4  fire_spirits8  firespirit_hut6  hog_rider5  mega_minion3  \
0           0              1                1           0             0   
1           1              0                0           1             1   

   minion8  rage4  skeleton_horde1  skeleton_horde3  valkyrie5  zapMachine1  
0        1      1                0                1          1            0  
1        0      0                1                0          1            1