I currently have two columns:
Word Sentence
apple [this, fruit, is, an, apple]
orange [orange, is, this, fruit]
grape [this, is, grape]
strawberry [strawberry, is, nice]
How would I go about removing the value that appears in df['Word'] from df['Sentence'] so that the output would be:
Word Sentence
apple [this, fruit, is, an]
orange [is, this, fruit]
grape [this, is]
strawberry [is, nice]
I am currently trying to use this while loop, which is not very pythonic.
count_row = df.shape[0]
i=0
while i < count_row :
mylist = df.iloc[i]["Sentence"]
mykeyword = df.iloc[i]["Word"]
mylist = mylist.split()
for word in mylist:
if word == mykeyword:
df.iloc[i]["Sentence"] = df.iloc[i]["Sentence"].replace(word, '')
print(i)
i=i+1
However, the loop is not removing the values. What is the best way to achieve the desired output?
How about something like...
def remove_name(r):
r['Sentence'] = [w for w in r['Sentence'] if w != r['Word']]
return r
df.apply(remove_name,axis=1)
Apply lets us perform operations like this all at once, no iterations required.
You can use remove function to remove an element from a list.
Syntax: list.remove(element)
Where 'list' is your sentence list and 'element' is your fruit name to be removed.
To know more about remove function refer python docs or this link: https://www.programiz.com/python-programming/methods/list/remove
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.