简体   繁体   中英

Compare same entry in two data frames in first column and move/add plus difference to the next column

I have two different downloads with all machines in a production and I want to forecast the production volume based on the available capacity. In case of more demand, the demand should be postponed to the next period and so on. If backlog is processed only demand should be forecasted. Eg first machine has not enough capacity in the first month, so from the demand of 300 only 250 can be produced --> move 50 to the following month, in the following month the demand is therefore 200 + 50 but the capacity is 220, so the forecast should be 220 and so on.

Example demand

df_demand = pd.DataFrame(np.array([[300, 200, 200, 180], [300, 150, 200, 150]]), columns=['April', 'May', 'June', 'July'])

Example capacity

df_cap = pd.DataFrame(np.array([[250, 220, 220, 250], [200, 200, 250, 200]]), columns=['April', 'May', 'June', 'July'])

How would you approach this?

No pythonic

def fun(d, c, r):
    # Given current demand, capacity and residual
    # find the currently meet demand and left over residual
    d = d + r
    if c >= d:
        return d, 0
    else:
        return c, d-c

forecast = []
for index, cap in df_cap.iterrows(): 
    if index not in df_demand.index:
        continue
    demand = df_demand.loc[index]
    forecast.append([])
    r = 0
    for i, c in enumerate(cap):
        d = demand[i]
        f, r = fun(d,c,r)
        forecast[-1].append(f)

print (pd.DataFrame(forecast, columns=df_cap.columns))

Output

   April  May  June  July
0    250  220   220   190
1    200  200   250   150

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM