I am stuck on the following problem:
I have this dataset (dummy values)
item price units
A 10 1
B 20 5
C 5 8
D 6 3
Out of it, I calculate the cumulative sum of both via:
threshold_price = 31
threshold_units = 8
ls = []
cumsum = 0
lastreset = 0
for _, row in df.iterrows():
if cumsum + row.price <= threshold_price:
cumsum += row.price
else:
last_reset = cumsum
cumsum = row.price
ls.append(cumsum)
df['cs_price'] = ls
ls = []
cumsum = 0
lastreset = 0
for _, row in df.iterrows():
if cumsum + row.units <= threshold_units:
cumsum += row.units
else:
last_reset = cumsum
cumsum = row.units
ls.append(cumsum)
df['cs_units'] = ls
From here I get:
item price units cs_price cs_units
A 10 1 10 1
B 20 5 30 6
C 5 8 5 8
D 6 3 11 3
I want to have a new column which we can call "Store" which would be equal to the first reset of any of the two columns and incrementing for each reset.
Given above example:
item price units cs_price cs_units store
A 10 1 10 1 0
B 20 5 30 6 0
C 5 8 5 8 1 #+1 because of price
D 6 3 11 3 2 #+1 because of units
The main issue I have through this approach is that, cs_price and cs_units will not reset when a new store is attributed, which is something I need.
Ideal output should be following:
item price units cs_price cs_units store
A 10 1 10 1 0
B 20 5 30 6 0
C 5 8 5 8 1 #+1 because of price, cs_units resets
D 6 3 6 3 2 #+1 because of units, cs_price resets
E 7 4 13 7 2
F 8 2 8 2 3
G 20 2 28 4 3
H 6 3 6 3 4
My current output (which is not what I want) is:
item price units cs_price cs_units store
A 10 1 10 1 0
B 20 5 30 6 0
C 5 8 5 8 1 #+1 because of cs_price
D 6 3 11 3 1
E 7 4 18 7 1
F 8 2 26 2 2 #+1 because of cs_units but cs_price does not reset
G 20 2 20 4 2
H 6 3 26 7 2
Full code (I did not include match columns above, I hope it is still readable):
threshold_price = 31
threshold_units = 8
ls = []
cumsum = 0
lastreset = 0
for _, row in df.iterrows():
if cumsum + row.price <= threshold_price:
cumsum += row.price
else:
last_reset = cumsum
cumsum = row.price
ls.append(cumsum)
df['cs_price'] = ls
ls = []
cumsum = 0
lastreset = 0
for _, row in df.iterrows():
if cumsum + row.units <= threshold_units:
cumsum += row.units
else:
last_reset = cumsum
cumsum = row.units
ls.append(cumsum)
df['cs_units'] = ls
df['match'] = df.cs_price < df.cs_price.shift()
df["match"] = df["match"].astype(int)
df['match2'] = df.cs_units < df.cs_units.shift()
df["match2"] = df["match2"].astype(int)
df["store_prep"] = df["match"].cumsum()
df["store_prep2"] = df["match2"].cumsum()
df["store"] = df[["store_prep","store_prep2"]].max(axis=1)