简体   繁体   中英

Delete specific value and subsequent row in a pandas df

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM