Home > database >  how to keep only two specific words after each other
how to keep only two specific words after each other

Time:05-17

I want my dataframe start with Login then followed with Logout, and continues with Login and Logout only.

for exammple (before):

enter image description here

output (after):

enter image description here

CodePudding user response:

First remove View rows and then filter consecutive Login, Logout rows:

df = df[df['B'].ne("View")]

m1 = df['B'].eq('Login') & df['B'].shift(-1).eq('Logout')
m2 = df['B'].eq('Logout') & df['B'].shift().eq('Login')

df2 = df[m1 | m2]

print (df2)
        B         C
0   Login  02:29:04
2  Logout  02:29:14
7   Login  02:38:45
9  Logout  02:39:05

CodePudding user response:

Try this code:

df.drop(df[df['B'] == "View"].index, inplace=True)
  • Related