Search code examples
pythonpandaslambdadata-analysisdata-cleaning

Remove values from one column, that are equal to the value in another


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?


Solution

  • 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.