i need to locate a value from a specific point in my dataframe backwards satisfying some condition. For example, the df below
index | ItemName | Price | Source | Cost
1 | toyHorse | 2.99 | Japan | 2.25
2 | toyDog | 2.50 | Cambodia | 1.80
3 | toyCat | 2.75 | China | 1.10
4 | toyHorse | 5.00 | Japan | 3.75
so first, I need to find toyHorse where price >= 5.00. Then starting from that row, looking backwards, I need find the immediate prior match where it’s toyHorse, Japan, and get the last cost of 2.25 and express it as a percentage of current sale price which is 2.25 / 5.00 in a new column called “PastPricePoint” so the desired output is below:
| ItemName | Price | PastPricePoint
| toyHorse | 5.00 | 0.45 ( calculated from 2.25/5.00)
it is important that as a filter for starting point of some price, the dataframe only looks at the immediate prior row that matches the criteria for this calculation.