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

久未见 提交于 2019-11-26 20:47:41

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