Trying my best to explain what I am trying to achieve here. I have 2 series (Annual Volume & Annual Capacity) which I need to compare to allocate a value in the column Allocation & then sum it cumulatively inside a group (L1 / L2 in Lane Column) until the Cum Allocation column matches **Annual Volume **Column.
The table looks like this:
| Lane | Carrier SCAC | Annual Volume | Annual Capacity | Allocation | Cum Allocation | |
|---|---|---|---|---|---|---|
| 25972 | L1 | BGME | 4917.0 | 2408.0 | 2408 | 2408 |
| 25973 | L1 | SCNN | 4917.0 | 3380.0 | 2459 | 4867 |
| 25974 | L1 | XPOL | 4917.0 | 4940.0 | 50 | 4940 |
| 25975 | L1 | SJRG | 4917.0 | 156.0 | ||
| 25976 | L1 | MTRK | 4917.0 | 4940.0 | ||
| 26055 | L2 | JNJR | 5604.0 | 260.0 | 260 | 260 |
| 26056 | L2 | WOSQ | 5604.0 | 1300.0 | 1300 | 1560 |
| 26057 | L2 | BGME | 5604.0 | 2704.0 | 2704 | 4264 |
| 26058 | L2 | ITSB | 5604.0 | 2080.0 | 1340 | 5604 |
| 26059 | L2 | UCSB | 5604.0 | 4368.0 |
The rule of allocation is as such:
Allocation = min(Annual Volume * 0.5, Annual Capacity) as long as Cum Allocation < Annual Volume
Allocation = (Annual Volume - Cum Allocation.shift(1)) so that Cum Allocation == Annual Volume
Allocation = 0 when Cum Allocation > Annual Volume
Cum Allocation = Cum Allocation.shift(1) + Allocation (initial Cum Allocation = Allocation)
I could do it through 2 functions - not quite efficient and the database is very large making it extremely slow & I need to keep changing allocation factor (0.5 above), making it quite painful.
I just have a feeling this can be handled through pandas standard functions itself. Any ideas would be helpful.
Cum Allocation>Annual Capacity? ShouldAllocation = 972andCum Allocation = 3380.0? (2) for the scond to last row, why isCum Allocation = 5604? How is that number calculated?