Reputation: 13
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:
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)
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)
Upvotes: 1
Views: 93
Reputation: 1804
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])
Upvotes: 2