【发布时间】:2018-08-08 20:01:02
【问题描述】:
我正在尝试将 Pyomo 模型输出放入 pandas.DataFrame 行。我现在通过将数据保存为.csv,然后将.csv 文件读取为DataFrame 来完成它。我想跳过.csv 步骤,直接将输出放入DataFrame。
当我使用 Pyomo 完成优化解决方案时,最佳分配是model.x[i] 输出数据中的1(否则为0)。 model.x[i] 由 dict 中的 v 键索引。 model.x 是 Pyomo 的特定语法
Pyomo 为每个提供最佳解决方案的值分配一个timeItem[i]、platItem[i]、payItem[i]、demItem[i]、v[i]。 0807results.csv 文件会生成最佳分配的准确文件,显示最佳解决方案中每个有效分配的 timeItem[i]、platItem[i]、payItem[i]、demItem[i]、v[i] 的值。
当model.x[i] 是1 时,如何将timeItem[i]、platItem[i]、payItem[i]、demItem[i]、v[i] 直接转换为DataFrame?非常感谢您的帮助。我当前的代码如下。
index=sorted(v.keys())
with open('0807results.csv', 'w') as f:
for i in index:
if value(model.x[i])>0:
f.write("%s,%s,%s,%s,%s\n"%(timeItem[i],platItem[i],payItem[i], demItem[i],v[i]))
from pandas import read_csv
now = datetime.datetime.now()
dtg=(now.strftime("%Y%m%d_%H%M"))
df = read_csv('0807results.csv')
df.columns = ['Time', 'Platform','Payload','DemandType','Value']
# convert payload types to string so not summed
df['Payload'] = df['Payload'].astype(str)
df = df.sort_values('Time')
df.to_csv('results'+(dtg)+'.csv')
# do stats & visualization with pandas df
【问题讨论】:
-
在您尝试编写它们之前,我没有看到
timeItem、platItem、payItem或demItem定义;是吗?
标签: python pandas append pyomo