encer
encer

Reputation: 55

Pandas DataFrames: How to delete rows in a dataframe based on a sequential comparison of their index values

I have a data frame named red_data_frame. Each row has a unique index value spanning from 1 to 54075. What I need to do is to iterate through each value and delete the row if it is immediately sequential. For example:

1
721
722
...

Delete 722

...
1442
1443
...

Delete 1443 etc...

The new dataframe should remove all rows that meet this condition.

Snippet of red_data_frame

Upvotes: 0

Views: 59

Answers (1)

BENY
BENY

Reputation: 323326

In your case try

out = df[df.index.to_series().diff().ne(1)]

Upvotes: 2

Related Questions