[英]Removing Custom-Defined Words from List - Python
我有一个 dataframe 列,如下所示:
我正在考虑删除特殊字符。 我希望附加标签(在列表列表中),以便我可以 append 该列到现有的 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)
输出一个字符串列表,因此您可以对其应用规则列表理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.