10

I am parsing a pandas dataframe df1 containing string object rows. I have a reference list of keywords and need to delete every row in df1 containing any word from the reference list.

Currently, I do it like this:

reference_list: ["words", "to", "remove"]
df1 = df1[~df1[0].str.contains(r"words")]
df1 = df1[~df1[0].str.contains(r"to")]
df1 = df1[~df1[0].str.contains(r"remove")]

Which is not not scalable to thousands of words. However, when I do:

df1 = df1[~df1[0].str.contains(reference_word for reference_word in reference_list)]

I yield the error first argument must be string or compiled pattern.

Following this solution, I tried:

reference_list: "words|to|remove" 
df1 = df1[~df1[0].str.contains(reference_list)]

Which doesn't raise an exception but doesn't parse all words eather.

How to effectively use str.contains with a list of words?

6
  • 1
    When you say "not scaleable", do you mean you would have a bunch of repetitive code? If so, use a loop: for reference_word in reference_list:
    – Galen
    Dec 22, 2017 at 7:51
  • Have you tried this question? Dec 22, 2017 at 7:51
  • I'd first join the words and pass them to str.contains.
    – cs95
    Dec 22, 2017 at 7:51
  • 1
    Can you elaborate on this: Which doesn't raise an exception but doesn't parse all words eather.? Can you provide an example that shows that it doesn't work? Because it should.
    – cs95
    Dec 22, 2017 at 7:53
  • 1
    @sudonym if you are looking for speed over regex I suggest you to go through Flasktext medium.freecodecamp.org/… for 10000x speed Dec 22, 2017 at 7:57

1 Answer 1

19

For a scalable solution, do the following -

  1. join the contents of words by the regex OR pipe |
  2. pass this to str.contains
  3. use the result to filter df1

To index the 0th column, don't use df1[0] (as this might be considered ambiguous). It would be better to use loc or iloc (see below).

words = ["words", "to", "remove"]
mask = df1.iloc[:, 0].str.contains(r'\b(?:{})\b'.format('|'.join(words)))
df1 = df1[~mask]

Note: This will also work if words is a Series.


Alternatively, if your 0th column is a column of words only (not sentences), then you can use df.isin, which should be faster -

df1 = df1[~df1.iloc[:, 0].isin(words)]
6
  • 1
    @sudonym You're welcome. Did you use contains or isin?
    – cs95
    Dec 22, 2017 at 8:04
  • str.contains, since I have sentences in iloc[:,0]
    – sudonym
    Dec 22, 2017 at 8:05
  • @cᴏʟᴅsᴘᴇᴇᴅ we should try to post an answer using flashtext it sounds promising. Dec 22, 2017 at 8:27
  • 1
    May I ask, what does this mean? r'\b(?:{})\b'
    – ah bon
    Jun 3, 2018 at 8:58
  • 1
    @ahbon I am inserting your search phrases inside a capturing group. I only want full words to be matched.
    – cs95
    Jun 3, 2018 at 20:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.