import pandas as pdfrom scipyoptimize import linprog# 读取数据df1 = pdread_excel附件1xlsxdf2 = pdread_excel附件2xlsxdf3 = pdread_excel附件3xlsxdf4 = pdread_excel附件4xlsxprintdf1columnsprintdf2columnsprintdf3colu
The error is occurring because the key '1' is not present in the index of the 'wholesale_prices' series.
To fix this, you can modify the range in the list comprehension to start from 0 instead of 1. Also, make sure that the column names in the 'df3' DataFrame match the index values in the 'wholesale_prices' series.
Here's the modified code:
# 提取所需数据
sales_data = df2[['单品编码', '销量(千克)']]
wholesale_prices = df3.set_index('单品编码')['批发价格(元/千克)']
loss_rates = df4.set_index('小分类编码')['平均损耗率(%)_小分类编码_不同值']
# 构建线性规划模型
n = len(df1)
c = [wholesale_prices[i] * (1 + 0.2) for i in range(n)] # Modified line
A_eq = [[1] * n]
b_eq = [27]
A_ub = [[-1] * n, [1] * n]
b_ub = [-2.5 * n, 33]
bounds = [(2.5, None)] * n
# 计算最优解
res = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds)
# 输出结果
replenishment_quantities = res.x
prices = [wholesale_prices[i] * (1 + 0.2) for i in range(n)] # Modified line
print("单品补货量:")
for i in range(n):
print(f"单品编码:{i+1},补货量:{replenishment_quantities[i]}")
print("定价策略:")
for i in range(n):
print(f"单品编码:{i+1},定价:{prices[i]}")
Make sure to check if the column names in the 'df3' DataFrame match the index values in the 'wholesale_prices' series.
原文地址: http://www.cveoy.top/t/topic/i1Q2 著作权归作者所有。请勿转载和采集!