我有一个数据框列,如下所示:
我正在考虑删除特殊字符。我希望附加标签(在列表列表中),以便我可以将列附加到现有的 df.
这是我收集了这么多,但似乎不起作用。正则表达式尤其给我带来了很大的痛苦,因为它总是返回“预期的字符串或类似字节的对象”。
df = pd.read_csv('flickr_tags_participation_inequality_omit.csv')
#df.dropna(inplace=True) and tokenise
tokens = df["tags"].astype(str).apply(nltk.word_tokenize)
filter_words = ['.',',',':',';','?','@','-','...','!','=', 'edinburgh', 'ecosse', 'écosse', 'scotland']
filtered = [i for i in tokens if i not in filter_words]
#filtered = [re.sub("[.,!?:;-=...@#_]", '', w) for w in tokens]
#the above line didn't work
tokenised_tags= []
for i in filtered:
tokenised_tags.append(i) #this turns the single lists of tags into lists of lists
print(tokenised_tags)
上面的代码不会删除自定义的停用词。
很感谢任何形式的帮助!谢谢!
你需要使用
df['filtered'] = df['tags'].apply(lambda x: [t for t in nltk.word_tokenize(x) if t not in filter_words])
注意nltk.word_tokenize(x)
输出一个字符串列表,以便您可以对其应用常规列表理解。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)