how to drop multiple sets of rows from a pandas dataframe

I’m trying to drop rows of a dataframe (spurs) that I’ve identified as erroneous.

I’m seeing strange behaviour.

spurs = [(16, 62), (72, 83)]

for spur in spurs:
    full_path.drop(full_path.index[spur[0]: spur[1]], inplace=True)

It will only drop the first spur from the dataframe when in the loop. If i do this:

full_path.drop(full_path.index[spurs[0][0]: spurs[0][1]], inplace=True)
full_path.drop(full_path.index[spurs[1][0]: spurs[1][1]], inplace=True)

I get the same behaviour. But if I drop them individually it works.

Any help would be appreciated.

Answer

You should avoid reshaping/dropping rows in the loop. Instead, collect all the indices to be dropped into a list and then drop them at once:

drop_indices = list()
for spur in spurs:
    drop_indices += list(full_path.index[spur[0]: spur[1]])

full_path = full_path.drop(drop_indices)