比较第一列中两个数据框中的相同条目和 move/add 加上与下一列的差异

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

我有两个不同的下载,所有机器都在生产中,我想根据可用容量预测生产量。如遇需求较多,应顺延至下一期需求等。如果处理积压订单,则只应预测需求。例如。第一台机器在第一个月没有足够的产能,所以从需求 300 只能生产 250 --> 将 50 移动到下个月,因此在下个月的需求是 200 + 50 但产能是 220,所以预测应为 220 等等。

示例需求

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

示例容量

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

你会如何处理这个问题?

没有 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))

输出

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