I am new to python and I faced a hard problem that I could not figure out how to do it. I need to compare values in two columns and then reorder the values based on this comparison. For example, let's assume we have the following dataframe. Now I need to check the values in B if they are smaller than the values in the cumulative sum of A. If a value in B higher than the value in cum of A then we stop at that row. And we reorder the observation based on the numerical value of column B until that row (the row is also included).
For instance, when we look at the values in B we see that all the values are smaller than the values in A until part 4.
| Part No | A | B | cum of A |
|---|---|---|---|
| 1 | 2 | 13 | 2 |
| 2 | 4 | 17 | 6 |
| 3 | 7 | 15 | 13 |
| 4 | 5 | 16 | 18 |
| 5 | 10 | 19 | 28 |
| 6 | 9 | 16 | 37 |
| 7 | 8 | 12 | 45 |
So that I need to reorder all values based on numerical values in B until part 4 (part 4 is also included). Then the new table should be
| Part No | A | B | cum of A |
|---|---|---|---|
| 1 | 2 | 13 | 2 |
| 3 | 7 | 15 | 9 |
| 4 | 5 | 16 | 14 |
| 2 | 4 | 17 | 18 |
| 5 | 10 | 19 | 28 |
| 6 | 9 | 16 | 37 |
| 7 | 8 | 12 | 45 |
And if there is still a higher value in cum of A than the values in B among these reordered observations (first four parts), then part 4 will be taken out from the list and put in a separate list.
This process should be repeated until all parts will be ordered.
Could anyone help me to get the code for this part?