38

I have a pattern:

patternDel = "( \\((MoM|QoQ)\\))";

And I want to delete all rows in pandas dataframe where column df['Event Name'] matches this pattern. Which is the best way to do it? There are more than 100k rows in dataframe.

1
  • 1
    Just to be clear, when you import columns with that string, they will be converted to "( \((MoM|QoQ)\))" unless they are raw strings. It would be helpful to include a sample of the data in order to get the best answer. Commented Oct 9, 2016 at 23:00

1 Answer 1

66

str.contains() returns a Series of booleans that we can use to index our frame

patternDel = "( \\((MoM|QoQ)\\))"
filter = df['Event Name'].str.contains(patternDel)

I tend to keep the things we want as opposed to delete rows. Since filter represents things we want to delete we use ~ to get all the rows that don't match and keep them

df = df[~filter]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works great. Probably shouldn't use the keyword "filter" as a variable name though, just to avoid confusion.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.