Quickest way to make a get_dummies type dataframe from a column with a multiple of strings

You can use:

>>> df['col2'].str.get_dummies(sep=',')
   A  B  C  G
0  1  1  0  0
1  1  0  1  1
2  0  1  0  0

To join the Dataframes:

>>> pd.concat([df, df['col2'].str.get_dummies(sep=',')], axis=1)
   col1   col2  A  B  C  G
0     6    A,B  1  1  0  0
1    15  C,G,A  1  0  1  1
2    25      B  0  1  0  0

Leave a Comment