How to assign values from a list of dictionaries to a 2-dimensional parameter in Pyomo

I know how to assign values from a single dictionary to a 1-dimensional parameter in Pyomo. For that I use the following code

import pyomo.environ as pyo
import pandas as pd

#Define the model
model = pyo.ConcreteModel()

#Read the dataframe from file for one building
df_buildingData = pd.read_csv("C:/Users/Data/H9_HH" + str(1) + "_Day_" + str(4) +".csv", sep =";")

#Define the sets
model.set_timeslots = pyo.RangeSet(1, 288)

#Create the dictionaries for the set
dictionaryHeatDemand_In_W= df_buildingData.set_index('Timeslot')['Q_htg'].to_dict()

#Assign the values from a single dictionary to a 1-dimensional parameter

model.param_heatDemand_In_W = pyo.Param(model.set_timeslots, initialize=dictionaryHeatDemand_In_W)
model.param_heatDemand_In_W.pprint()


and I get the following output

So far so good. However, now I would like to have a 2-dimensional parameter that is based on the set "model.set_timeslots" and the parameter "model.set_buildings". To do that I used the same code but I changed it such that I always work with lists (one entry for every building). Here is the code for the 2-dimensional parameter:

import pyomo.environ as pyo
import pandas as pd

#Define the model
model = pyo.ConcreteModel()

list_df_buildingData_mHP_EV= [pd.read_csv("C:/Users/Data/H9_HH" + str(index) + "_Day_" + str(4) +".csv", sep =";") for index in range (1,  4)]

#Define the sets
model.set_timeslots = pyo.RangeSet(1, 288)
model.set_buildings = pyo.RangeSet(1, 3)

#Create the dictionaries for the set
list_dictionaryHeatDemand_In_W_mHP_EV= [list_df_buildingData_mHP_EV[index].set_index('Timeslot')['Q_htg'].to_dict() for index in range (0,  3)]

#Define the parameters of the model in pyomo
model.param_heatDemand_In_W = pyo.Param(model.set_timeslots,model.set_buildings, initialize=list_dictionaryHeatDemand_In_W_mHP_EV)
model.param_heatDemand_In_W.pprint()


However, the outcome when using model.param_heatDemand_In_W.pprint() does not like the one I would like to have. Here you see a screenshot. There is always a key values at the beginning starting from (1,1) to (288,3). For each of those key values there are 288 entries (288 is the number of set_timeslots). I would like to have a 2-dimensional parameter param_heatDemand_In_W (building, timeslot). So the parameter should have for each of the 3 buildings, 288 timeslots and for each of those timeslots there should be 1 value for the heatDemand.

Any idea how I can do that? I'd appreciate every comment.

You can put your data in a dataframe after reading it from a csv file then define your model as abstract then you can do two things 1- initializing your parameter as shown in the figure
or

def init_w(model,i,j):
return df.iloc[i-1,j-1]
model.W =Param(model.i, model.j, mutable=True, initialize=init_w)


2- filling the instance properly

• Thanks for your answer Optimization Team. You wrote: "You can put your data in a dataframe". The question is how can I do that. I have a list of dataframes as you can see in the second screenshot. Each of those dataframes has the column "Q_htg" (as you can see in the first screnshot). Now the question is how to create a new dataframe that has as index "Timeslots" and the columns should be the Q_htg value for every builiding. – PeterBe May 27 at 9:38
• I tried 2 approaches in order to achieve what you suggested but both of them failed: series = [pd.Series(list_df_buildingData_mHP_EV[index][:, 1]) for index in range (0, len(list_df_buildingData_mHP_EV))] newDataframe = pd.concat(series, axis=1) --> "TypeError: '(slice(None, None, None), 1)' is an invalid key". And the second approach is for index in range (0, len(list_df_buildingData_mHP_EV)): newDataFrame[index] = list_df_buildingData_mHP_EV[index] ["Q_htg"] --> "NameError: name 'newDataFrame' is not defined" – PeterBe May 27 at 9:42
• It is clearly shown in my answer that you can put your data in a csv and read it with pandas then follow the rest – Optimization team May 27 at 11:36
• Thanks Optimization team for yoru answer. Basically I now managed to create this new dataframe with all the values from the different building and I used your approach and it seems to work. I have 2 questions about this: 1) Why do you need to use i-1, j-1 and not i,j (so why do you have to substract one 2) Why do you have to use the iloc function return df.iloc[i-1,j-1] and not just return df [i-1} [j-1]? – PeterBe May 27 at 16:28
• Thanks for your answer and effort Optimization team. I really appreciate it. I upvoted and accepted your answer. – PeterBe May 28 at 10:32