python - 比较第一列中两个数据框中的相同条目,并将差异移动/添加到下一列
问题描述
我对生产中的所有机器都有两个不同的下载,我想根据可用容量预测生产量。如果需求量增加,则将需求推迟到下一个周期,以此类推。如果处理积压,则只应预测需求。例如第一台机器第一个月产能不够,所以从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'])
你会如何处理这个问题?
解决方案
没有蟒蛇
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
推荐阅读
- python - 如何在 plotly.express 直方图中增加 x 刻度
- c++ - 使用 wxWidget 构建“多界面”
- c# - 如何在单例中使用作用域依赖注入
- graphql - 在 WSO2 上发送 GraphQL 内省请求
- python - 地形数据,字符串“-”不能转换为浮点数
- amazon-web-services - AWS CodePipeline - 在 CodePipeline 中部署我的 .NET Web api 时出错
- python - Python获取excel列大小
- django - Django:检查用户输入的旧密码是否有效
- reactjs - 使用 react fetch 在 URL 中间添加查询参数
- spring-tool-suite - 在 STS Boot 仪表板中安排项目