我有一个像这样的 df:
text
hello how are you
hello people
hello stackoverflow
和这样的列表:
单词= [“你好”,“人们”,“stackoverflow”]
预期输出:
text Hello people stackoverflow
hello how are you 1 0 0
hello people 1 1 0
hello stackoverflow 1 0 1
Use Series.str.get_dummies http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.get_dummies.html with DataFrame.reindex http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html用于按列表过滤列(值必须小写才能匹配)和最后一个DataFrame.join http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.join.html至原文:
words = ["hello","people", "stackoverflow"]
df1 = df.join(df['text'].str.get_dummies(' ').reindex(columns=words))
print (df1)
text hello people stackoverflow
0 hello how are you 1 0 0
1 hello people 1 1 0
2 hello stackoverflow 1 0 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)