簡體   English   中英

從 Pandas 系列中刪除文字在文本中出現少於 2 次

[英]Remove Words appears less than 2 times in text' from Pandas Series

我正在嘗試刪除 Pandas 系列中每個標量值中出現的所有單詞。 最好的方法是什么? 這是我失敗的嘗試:

 from collections import Counter df = pd.DataFrame({'text':["The quick brown fox", "jumped over the lazy dog","jumped over the lazy dog"]}) d=''.join(df['text'][:]) m=d.split() q=Counter(m) print (q) df['text'].str.split().map(lambda el: " ".join(Counter(el for el in q.elements() if q[el] >= 2)))

 out put: Counter({'over': 2, 'the': 2, 'lazy': 2, 'The': 1, 'quick': 1, 'brown': 1, 'foxjumped': 1, 'dogjumped': 1, 'dog': 1}) 0 over the lazy 1 over the lazy 2 over the lazy Name: text, dtype: object

from collections import Counter

df = pd.DataFrame({'text':["The quick brown fox", "jumped over the lazy dog","jumped over the lazy dog"]})
c = Counter(df.text.str.split().explode())
print( df.text.apply(lambda x: ' '.join(w for w in x.split() if c[w] >= 2).strip()) )

印刷:

0                            
1    jumped over the lazy dog
2    jumped over the lazy dog
Name: text, dtype: object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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