Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete rows in python pandas DataFrame using regular expressions?

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.

like image 698
Alexander Avatar asked Oct 09 '16 21:10

Alexander


People also ask

How do I delete a row in pandas Dataframe?

To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.

How do I delete rows in pandas Dataframe based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do I delete 10 rows in pandas?

You can delete a list of rows from Pandas by passing the list of indices to the drop() method. In this code, [5,6] is the index of the rows you want to delete. axis=0 denotes that rows should be deleted from the dataframe.

How do I remove a specific row from a Dataframe in Python?

To drop a specific row from the data frame – specify its index value to the Pandas drop function. It can be useful for selection and aggregation to have a more meaningful index. For our sample data, the “name” column would make a good index also, and make it easier to select country rows for deletion from the data.


1 Answers

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] 
like image 143
Bob Haffner Avatar answered Sep 26 '22 00:09

Bob Haffner