# How to display results of Pyomo variables in a pandas dataframe

I would like to display all the results of Pyomo variables in a pandas dataframe. I had a look at this question Pyomo: Looping Over A Variable Method and tried to adjust it to my case without success. I always get error messages. Here is a part of my code:

model.set_timeslots = pyo.RangeSet(1, 288)
iterables = list (range (1, 288))
...
...
...
for v in model.component_objects(pyo.Var, active=True):
for index in v:
print(index, ': ', pyo.value(v[index]))

DF = pd.DataFrame(index=iterables)
for v in model.component_objects(pyo.Var, active=True):
for index in v:
DF.at[index, 'val'] = pyo.value(v[index])


So I would like to print each value of the variables for all 288 set values for the set model.set_timeslots. Further, I also have some variables that do not depend on a set (for example objective variables). I would also like them to be displayed in a pandas dataframe. Do you know how I can do that? I'd appreciate every comment and would be quite thankful for your help.

Reminder: Does nobody have an idea how I can do that?

• Would you try using this or this as offered by Pyomo? I hope they will be helpful. :) Apr 20 '21 at 10:40
• Thanks for your comment A.Omidi. So shall I not ask questions about Pyomo in the Operations Research channel? I have seen quite many questions about Pyomo here. Apr 20 '21 at 11:51
• OR.SE is an optimization forum and actually, you can ask your related questions here. What I mentioned is two useful links offered by Pyomo developers in its host. You might get the answer to your technical questions there a bit quickly 😉 Apr 20 '21 at 19:31

I don't know your entire code or the errors you get, but the general way is

optimal_values = [value(model.x[key]) for key in model.x]
df = pd.DataFrame(optimal_values)


where

model.x


is your target variable that is optimized.

• Thanks Steven for your answer. Unfortunately I get an error message when using your suggested code optimal_values = [value(model.variable_heatGenerationCoefficient_SpaceHeating[key]) for key in model.variable_heatGenerationCoefficient_SpaceHeating] df = pd.DataFrame(model.variable_heatGenerationCoefficient_SpaceHeating) stating "NameError: name 'value' is not defined". Apr 21 '21 at 9:32
• Further, I have quite many variables in my model (about 30). Is there no way how I can just get the values for all of them together e.g. via a loop? With your approach I would have to specify this for each of the 30 variables which is quite time consuming Apr 21 '21 at 9:33
• your second question: yes ofc, you can put all the variables in a list and then loop over the list (e.g. for model in model_list). I don't get why you get that name error (has something to do with your variable), maybe it's good to share you entire code or at least the crucial parts. Apr 21 '21 at 9:40
• I don't know what they mean with that error, but the part "for model.model_item in outputVariablesList" should be "for model_item in outputVariablesList" Apr 22 '21 at 7:35
• Thanks a lot Steven for your tremendous help and effort. It helped a lot. I upvoted and accepted your answer Apr 22 '21 at 15:53