0

I have a data frame with keywords and a list of words which if exist in that data frame then should be deleted.

Only the word should be deleted not the column or row from the data frame.

Here is the Dataframe I have. I have a list of words as well as a pandas series of it. I want to drop only the words which are on that list from the keywords.

enter image description here

2
  • It will be better if you can post your data frame and your expected data frame Commented Oct 28, 2017 at 3:09
  • 3
    Possible duplicate of Delete rows based on list in pandas Commented Oct 28, 2017 at 3:14

1 Answer 1

0

You could loop through it like this:

import pandas as pd

df = pd.DataFrame([['www.test.com', ['test', 'joke']],
                   ['www.google.com', ['jacket', 'coffee', 'second']],
                   ['www.stackoverflow.com', ['word', 'another']]],
                   columns = ['URL', 'keywords'])

values = ['test', 'ten', 'second', 'stackoverflow']


for index, row in df.iterrows():
    for word in row['keywords']:
        if word in values:
            row['keywords'].remove(word)

print(df)

#output

                     URL          keywords
0           www.test.com            [joke]
1         www.google.com  [jacket, coffee]
2  www.stackoverflow.com   [word, another]
Sign up to request clarification or add additional context in comments.

5 Comments

This code gives every single value from the dataframe. But unable to enter the if statement.
Works for me. See the full example given. Update above.
I did not want to delete the whole row. Want to delete just the keyword from its list.
@karancraze. Now try it
Thanks a lot. It works now. You saved me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.