簡體   English   中英

從列表中刪除自定義詞 - Python

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM