Reputation: 366
I want to delete
specific rows
in a pandas
df
. I want to remove a row
based on an undesired value plus the subsequent row
. For the following df
I want to remove the row
where Code == 'Cat'
plus the subsequent row
. Below is my attempt.
import pandas as pd
import pandas as pd
d = ({
'Code' : ['Foo','Bar','Cat','Foo','Foo'],
'Int' : ['x','y','a','a','x'],
})
df = pd.DataFrame(d)
df = df[df.Code.shift()!='Cat']
Code Int
0 Foo x
1 Bar y
2 Cat a
4 Foo x
Intended Output:
Code Val
0 Foo x
1 Bar y
2 Foo x
Upvotes: 2
Views: 141
Reputation: 18647
Use boolean indexing
with ~
operator (logical NOT) and |
operator (logical OR):
df[~(df.Code.eq('Cat') | df.Code.shift(1).eq('Cat'))]
Code Int
0 Foo x
1 Bar y
4 Foo x
Upvotes: 2
Reputation: 71590
One other way:
df = df[~((df['Code'] == 'Cat') | (df['Code'].shift(1) == 'Cat'))]
And now:
print(df)
Is:
Code Val
0 Foo x
1 Bar y
4 Foo x
Upvotes: 1