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)
1
  • Just my_list.remove(x). This works in-place i.e. it modifies the list it is called on Commented Feb 3, 2019 at 20:59

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.