Search code examples
pythonnltkpython-re

KeyError: 53 when using re module


I was trying to substitute every thing else with a blank using the code :

corpus_test = []
ps = PorterStemmer()
for i in range(len(texts)):
    review = re.sub('[^a-zA-Z]',' ',texts['title'][i])
    review = review.lower()
    review = review.split()
    review = [ps.stem(word) for word in review if not word in stopwords.words('english')]
    review = ' '.join(review)
    corpus_test.append(review)

But I got the following error :

KeyError: 53

Which is from the use of re module :

enter image description here


Solution

  • The problem is that I was working with a csv file and I dropped some rows but I didn't reset the index, thus when we reach the iteration nb 53 i.e the index 53 we don't find it because it was dropped.

    texts.reset_index(inplace = True, drop = True)