I have a dataframe:
df = c1 c2 c3 code
1. 2. 3. 200
1. 5. 7. 220
1. 2. 3. 200
2. 4. 1. 340
6. 1. 1. 370
6. 1. 5. 270
9. 8. 2. 300
1. 6. 9. 700
9. 2. 1. 200
8. 1. 2 400
1. 2 1. 200
2. 5. 3 900
8. 0. 4. 300
9. 1. 2. 620
I want to take only the rows that are between any row with 300 code to its previous 200 code. So here I will have
df. c1 c2 c3 code batch_num
1. 2. 3. 200. 0
2. 4. 1. 340. 0
6. 1. 1. 370. 0
6. 1. 5. 270. 0
9. 8. 2. 300. 0
1. 2 1. 200. 1
2. 5. 3 900. 1
8. 0. 4. 300. 1
So basically what I need is to: find each 300, and for each - find the nearest previous 200, and take the rows between them. It is guaranteed that there will always be at least one 200 before each 300. Than, add a columns that indicate the proper batch. How can I do it efficiently in pandas?