Python Forum
How to extract previous and current rows if their value is equal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extract previous and current rows if their value is equal
#1
Hi,

I want to extract rows if the previous row of Name column equal to 'MM' and current row of Name column is 'MA'

import pandas as pd
import numpy as np
df=pd.DataFrame({'Name':['MM','MA','CS','MM','MM','TH','MM','MA'],'Rank':[1,3,3,1,2,3,2,2],'Score':[120,20,12,15,2,83,22,32]})
for i,(index,row) in enumerate(df.iterrows()):
    if i == 0: continue
    df[(df.Name.loc[i]=='MA') & (df.Name.loc[i-1]=='MM')]
Reply


Messages In This Thread
How to extract previous and current rows if their value is equal - by SriRajesh - Jun-18-2018, 02:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  RNG; One variable must not be equal to another DavidRobinsons 12 5,871 Oct-10-2018, 02:43 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020