test = df['Sanctions']== 'Sanction' #create a true/false list of rows that = 'Sanction
test1 = test.shift(-1) #shift list by 1 to get previous row
test2 = test.shift(-2) #shift list by 2 to get 2nd previosu row
df[(test == True) |(test1==True) | (test2==True)] #select rows from df where any of test or test1 or test2 are true
我想你想要的是
shift
:我想这会给你你想要的。(尽管我个人认为不依赖于排序结果的方法会更好。)
相关问题 更多 >
编程相关推荐