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.
To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With