title: How can I export results from a Pyomo variable to a Pandas dataframe and excel?
body: <p>I have a few Pyomo variables in a model, some with three indices. For the purpose of explaining, one of my variables is </p>
  
  <pre><code>model.E_grid = Var(model.i, model.m, model.p, within = NonNegativeReals)
  </code></pre>
  
  <p>and the three indices are i, m and p. After running the model, I would like to see the final values of E_grid with respect to each i, m and p in an pandas dataframe (which will then allow me to export it to excel).
  So for example, something like <em>[i1, m1, p1, 21.00], [i1, m1, p2, 22.00]</em>, and so on. I have already seen the "block" method which tries to export all the variables in one go (e.g. <a href="https://or.stackexchange.com/questions/2708/pyomo-looping-over-a-variable-method">https://or.stackexchange.com/questions/2708/pyomo-looping-over-a-variable-method</a>) but it doesn't work for me because some of the variables have just 1 index. 
  Any help with this would be much appreciated!</p>
  
  <p>Edit: this is specifically what I have tried</p>
  
  <pre><code>results_df = pd.DataFrame()
  for v in model.component_objects(model.E_grid, active=True):
      for i, m, p in v:
          results_df.at[i,m,p, v.name] = value(v[i,m,p])
  
  print(results_df) 
  </code></pre>
  
  <p>But I get the error <em>ValueError: Not enough indexers for scalar access (setting)!</em></p>