Skip to content
Advertisement

Deleting consecutive rows in a pandas dataframe with the same value

How can I delete only the three consecutive rows in a pandas dataframe that have the same value (in the example below, this would be the integer “4”).

Consider the following code:

JavaScript

I would like to get the following result as output with the three consecutive rows containing the value “4” being removed:

JavaScript

Advertisement

Answer

first get a group each time a new value exists, then use GroupBy.head

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement