Reputation: 2562
I am trying to figure out if there is a simple way to solve the following problem with pandas. Let's say I have four containers, A, B, C, D and each has a specific volume. Let's say I now get an amount of liquid that I want to fill these containers with. How can I come up with a "series" that shows the amount that each container got filled with. Essentially, I want the liquid to fill in container A, then container B, etc until there is no more liquid left. Not all containers have to fill up and there is never more liquid than there are containers. Here is the problem:
Container Volume (L) FILLED
A 6 0
B 7 0
C 8 0
D 9 0
Case A:
Liquid = 10L
Result:
Container Volume (L) FILLED
A 6 6
B 7 4
C 8 0
D 9 0
Case B:
Liquid = 20L
Result:
Container Volume (L) FILLED
A 6 6
B 7 7
C 8 7
D 9 0
I have played around with pandas.cumsum() and pandas.clip_upper()
but it takes me a few steps to get to where I need and was hoping there is a slick way to do it. Like I can do for case B something like:
filled_a = df["Volume"].cumsum().clip_upper(20) and continue mangling until I get to my intended result
Upvotes: 1
Views: 106
Reputation: 11963
a = df["Volume"].cumsum().clip_upper(20)
b = a.shift().fillna(0)
filled_a = a - b
Upvotes: 1