列中具有多个标签的一种热编码

Nav*_*Raj 2 python dataset pandas one-hot-encoding

我有一个简单的数据集。

id,question,category,tags,day,quarter,group_id

1,What is your name,Introduction,Introduction,1,3,0

2,What is your name,Introduction,"Introduction, work",1,3,1
Run Code Online (Sandbox Code Playgroud)

现在,如果您看到,该tags列中有多个用逗号分隔的输入。如果我尝试使用pandas 函数进行单热编码,get_dummies我会将其作为一列。但我想为每个标签创建列。我怎样才能做到这一点?

jez*_*ael 7

我相信需要str.get_dummies

df1 = df['tags'].str.get_dummies(', ')
print (df1)

   Introduction  work
0             1     0
1             1     1
Run Code Online (Sandbox Code Playgroud)