DickyS
DickyS

Reputation: 115

Drop a row depending on the content of the row after it

I have a dataframe like this:

   A B C
0  1 0 0
1  1 1 1
2  1 0 0
3  1 0 0
4  1 1 1
5  1 0 0

How do I remove a row based on the contents of the row after it? I only want to keep the rows where the row below is 1 1 1 and remove anything where it is 1 0 0 or doesnt exist. So in this case row 2 and 5 would be dropped.

Upvotes: 0

Views: 35

Answers (2)

zipa
zipa

Reputation: 27869

To get rows that meet your requirements you can use:

df[df.shift(-1).apply(tuple, axis=1)==(1,1,1)]
#   A  B  C
#0  1  0  0
#3  1  0  0

Or this one to get rows 2 and 5:

df[df.shift(1).apply(tuple, axis=1)==(1,1,1)]
#   A  B  C
#2  1  0  0
#5  1  0  0

Or if 2 and 5 get dropped this will make it happen:

df[(df.shift(-1).apply(tuple, axis=1)==(1,1,1))|(df.apply(tuple, axis=1)==(1,1,1))]
#   A  B  C
#0  1  0  0
#1  1  1  1
#3  1  0  0
#4  1  1  1

Upvotes: 0

BENY
BENY

Reputation: 323276

You can using shift with eq and all

df[(df.eq(1).all(1))|(df.eq(1).all(1).shift(-1))]
Out[228]: 
   A  B  C
0  1  0  0
1  1  1  1
3  1  0  0
4  1  1  1

Update

s=df.astype(str).apply(','.join,1)
df[(s=='1,1,1')|((s=='1,1,1').shift(-1))|(s!='1,0,0')]
Out[237]: 
   A  B  C
0  1  0  0
1  1  1  1
3  1  0  0
4  1  1  1

Upvotes: 1

Related Questions