Problem:
What'd I like to do is step-by-step reduce a value in a Series by a continuously decreasing base figure.
I'm not sure of the terminology for this - I did think I could do something with cumsum and diff but I think I'm leading myself on a wild goose chase there...
Starting code:
import pandas as pd
ALLOWANCE = 100
values = pd.Series([85, 10, 25, 30])
Desired output:
desired = pd.Series([0, 0, 20, 30])
Rationale:
Starting with a base of ALLOWANCE - each value in the Series is reduced by the amount remaining, as is the allowance itself, so the following steps occur:
- Start with 100, we can completely remove
85so it becomes0, we now have15left asALLOWANCE - The next value is
10and we still have15available, so this becomes0again and we have5left. - The next value is
25- we only have5left, so this becomes20and now we have no further allowance. - The next value is
30, and since there's no allowance, the value remains as30.
valuesvariable intoexpensesand thedesiredvariable intodebts, which in combination withallowancemakes the reader understand what you are trying to accomplish without even looking at the text, imo.