简体   繁体   English

比较第一列中两个数据框中的相同条目,并将差值移动/添加到下一列

[英]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.比如第一台机器第一个月产能不够,所以从300的需求只能生产250-->把50移到下个月,所以下个月的需求是200+50但产能是220,所以预测应该是 220 等等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为第一个数据帧的每一列计算两个数据帧的差异 - Calculating difference of two data frames for each column of first data frame 按 python 中的每一列比较两个数据帧? - Compare two data frames by each column in python? 比较两个数据框pyspark中的列名 - Compare column names in two data frames pyspark 比较具有不同列名的两个数据框,并使用来自第二个数据框的列更新第一个数据框 - Compare two data-frames with different column names and update first data-frame with the column from second data-frame 比较两个数据框,然后根据另一个将新列添加到其中一个数据框 - Compare two dataframes, and then add new column to one of the data frames based on the other 比较两个数据帧并根据掩码值将新列添加到数据帧 - compare two data frames and add new column to dataframe based on mask values 滚动两个数据框并比较一列数据 - Scroll through two data frames and compare a column of data 想要使用列值比较两个数据框 - want to compare two data frames using a column value 如何基于列比较两个不同大小的数据框? - How to compare two data frames of different size based on a column? 如何将具有相同列值的两个熊猫数据框合并以形成显示值差异的第三个数据框 - How two panda data frames with same column values can be merged to form the third data frame that shows the difference of the values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM