1

I have a list with this shape:

temp5=[]
for i in range(0,len(df)):
   temp5.append(df['text'][i].split())
df['each']=temp5
df['each']

and the result is like this:

enter image description here

now I want to remove some elements of the previous list. I want to check if each word of the previous list is similar to the following list, remove it from it. the second list is like this:

stopwords = open('stop_words.txt','r').read().split('\n')
print(stopwords)

enter image description here

now I wrote this code to delete the same words of each list from the first one. but all I receive is NONE. Could you please help me with it?

for k in range(0,len(df)):
    for j in df['each'][k][:]:
        for f in stopwords:
            if f==j:
                temp6.append(df['each'][k][:].remove(f))
                print(temp6)
Barmar
  • 741,623
  • 53
  • 500
  • 612
john
  • 13
  • 2

1 Answers1

2

As mentioned in the comments, remove method removes inplace, but if you want something more 'pythonic', the working code would be

temp5=[]
for i in range(0,len(df)):
    temp5.append([x for x in df['text'][i].split() if x not in stopwords])

using the list comprehension as mentioned e.g. in this question, which creates the filtered list. Or, if you insist on using the original dataframe as input, it would be something like

temp5=[]
for i in range(0,len(df)):
    temp5.append([x for x in df['each'][i] if x not in stopwords])
Matěj Račinský
  • 1,679
  • 1
  • 16
  • 28