Remove Words在文字中从熊猫系列中出现的次数少于2次

问题描述 投票:0回答:1

我正在尝试删除从熊猫系列的每个标量值中出现的所有单词。最好的方法是什么?这是我失败的尝试:

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
python dictionary counter
1个回答
0
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.