I am trying to remove all occurrences of the words I collect in the list exclude
from every text note. This is my approach.
import operator,re
exclude=[]
for i in range(len(freq_select)):
if freq_select[i][1]<=25:
exclude.append(freq_select[i][0])
def remove_nonfrequent(note):
print(remove_nonfrequent.i)
sentences=[]
for sent in note.split('.'):
words=sent.split(' ')
#print("Before:",len(words))
for word in words:
for j in exclude:
if j==word:
#print(j,word)
words.remove(word)
#print("After:",len(words))
sent=' '.join(words)
sentences.append(sent)
note='. '.join(sentences)
remove_nonfrequent.i+=1
return '. '.join([i for i in note.split('.') if i!=''])
remove_nonfrequent.i=0
jdf.NOTES=jdf.NOTES.apply(remove_nonfrequent)
I was initially looping over all the notes in pandas series. But now I am using apply()
and I can say performance increased little bit. But it still takes a very long time.
Each note in the pandas.Series
is of variable length. However, an average note can contain somewhere between 3000-6000 words. One note even has 13000 words.
I would like to get some help in this.