Reputation: 497
simple question but I have not been able to find anything on this. I have the following dataframe, and hope to accomplish the following:
where Roll = 1 in our dataframe, I want to delete the next row, and only that one row. Keep in mind that many rows for Roll will be 0 but only a few need to be removed specifically rows where the preceding row for Roll = 1
Date Close Open Roll
1 500 499 0
2 502 502 0
3 500 499 0
4 500 499 1
5 501 502 0 (delete this row)
6 506 506 0
7 509 508 0
Upvotes: 1
Views: 133
Reputation: 394389
Create a boolean mask using shift
that tests whether the previous row contains 1 or not:
In [24]:
df[df['Roll'].shift() != 1]
Out[24]:
Date Close Open Roll
0 1 500 499 0
1 2 502 502 0
2 3 500 499 0
3 4 500 499 1
5 6 506 506 0
6 7 509 508 0
the boolean mask looks like this:
In [25]:
df['Roll'].shift() != 1
Out[25]:
0 True
1 True
2 True
3 True
4 False
5 True
6 True
Name: Roll, dtype: bool
Upvotes: 3