Jun-18-2018, 02:49 PM
Hi,
I want to extract rows if the previous row of Name column equal to 'MM' and current row of Name column is 'MA'
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')]