title: How to extract Indexed Variable information in pyomo Model and build Pandas Dataframe
body: <p>I am attempting to solve a MIP using Pyomo with several Indexed Variables of different sizes. What I would like to do is export the results of the variables to a dataframe so that I can further analyze these results. My problem is that I cannot figure out a way to nicely automate the process as the size of the Indexed Variables can change in each model simulation.</p>
  <p>Here is an example of some variables I would create:</p>
  <pre><code>import pyomo.environ as pyo
  
  model = pyo.ConcreteModel()
  
  model.T = pyo.RangeSet(0, 10)
  model.Generators = pyo.Set(initialize=(['equip_1', 'equip_2']))
  model.Storages = pyo.Set(initialize=(['storage_1', 'storage_2']))
  
  model.var_1 = pyo.Var(model.T, model.Generators, domain=pyo.NonNegativeReals)
  model.var_2 = pyo.Var(model.T, domain=pyo.NonNegativeReals)
  model.var_3 = pyo.Var(model.T, model.Storages, domain=pyo.NonNegativeReals)
  model.var_4 = pyo.Var(model.T, model.Generators, domain=pyo.Binary, initialize=0)
  
  # constraints and objective function here, which I don't think are relevant for the question
  .
  .
  .
  SolverFactory('cbc').solve(model).write()
  </code></pre>
  <p>Now, I want to create the dataframe with model.T as the index and the variable name plus the model.Generator or model.Storages as a multiIndex column (I am assuming it would have to be a multiIndex, but perhaps not). A crude example of how I want it to look is shown below:</p>
  <pre><code>  |      Var_1      | Var_2 |        Var_3        |      Var_4
    | equip_1 equip_2 | None  | storage_1 storage_2 | equip_1 equip_2
  m |   0    |   0    |   0   |     0     |   0     |   0    |   1  
  o |   1    |   1    |   1   |     1     |   1     |   1    |   0  
  d |   2    |   2    |   2   |     2     |   2     |   0    |   1  
  e |   3    |   3    |   3   |     3     |   3     |   1    |   0  
  l |   4    |   4    |   4   |     4     |   4     |   0    |   1  
  . |   5    |   5    |   5   |     5     |   5     |   1    |   0  
  T |   6    |   6    |   6   |     6     |   6     |   0    |   1  
  </code></pre>
  <p>The values shown are just examples. Also, the model.T doesn't have to be the index as it would correlate to a standard index created with a dataframe.</p>
  <p>I have been pulling my hair out trying to get this to work but I am struggling to find the right syntax in pyomo. I feel there must be an easy way to extract this data as I can't be the first one trying to do this but I've searched the internet to no avail for this specific problem. The problem I have is trying to extract the variable index (model.T and model.Generators/Storages) from the variable results in an easy loop or vectorized fashion.</p>
  <p>Please! Any suggestions would be greatly appreciated. Let me know if I haven't made this clear enough.</p>