Python 如何为pandas中的每个字符串单词创建列
我有这样一个数据帧:Python 如何为pandas中的每个字符串单词创建列,python,pandas,Python,Pandas,我有这样一个数据帧: `id` `text` 1 Hello world how are you 2 Hello people I am fine 3 Good Morning 4 Good Evening 我想记下每个单词,并为它们制作不同的专栏。它们将只包含两个值1或0(1表示文字中存在单词,0表示nope) 预期产出: `id` `text` Hello world how are you people I am fi
`id` `text`
1 Hello world how are you
2 Hello people I am fine
3 Good Morning
4 Good Evening
我想记下每个单词,并为它们制作不同的专栏。它们将只包含两个值1或0(1表示文字中存在单词,0表示nope)
预期产出:
`id` `text` Hello world how are you people I am fine Good Morning Evening
1 Hello world how are you 1 1 1 1 1 1 1 1 1 1 1 1
2 Hello people I am fine 1 0 0 0 0 1 1 1 1 0 0 0
3 Good Morning 0 0 0 0 0 0 0 0 0 1 1 0
4 Good Evening 0 0 0 0 0 0 0 0 0 1 0 1
这是
get\u dummies
pd.concat([df,df.text.str.get_dummies(' ')],axis=1)
用于:
需要
df.join(df.text.str.get_dummies(“”))
如何连接这两个数据帧?pd.concat([df,df.text.str.get_dummies(“”)],axis=)
@johndoe@Wen-Ben Hi Wen,我对我的数据科学抱负(关于numpy)有一些问题,我可以在哪里给你发电子邮件/与你联系?@anky_91目前我没有stack的电子邮件,但你可以随时在这里ping我,甚至我们可以创建一个聊天室:-)@anky_91 fornumpy
如果你想在stack中学习它,我建议你查看一些高投票率的答案,也即将提到,numpy和scipy对MLI来说都很重要。我有更多重复的重复列表。@Wen Ben-hmm,idea-我可以将重复列表添加到我的个人资料中。我认为最好在你的文件中列出你的大部分著名答案,因为你的回答总是很好,简洁,涵盖了大多数常见的问题:-)我问了5-6个关于熊猫的问题,你总是回答。stackoverflow是你的全职工作吗?如果没有,那你不觉得无聊吗?除了stackoverflow之外,你还会做其他事情吗P只是好奇呵呵,你的工作做得很好,非常感谢。。seriously@anky_91-我宁愿过滤更长一点的列表;)
df1 = df.join(df.text.str.get_dummies(sep=' '))