ampl / amplpy

Python API for AMPL

Home Page:https://amplpy.ampl.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

amplpy issues with getVariable

muv1023 opened this issue · comments

I have a three dimensional variable. I need to do some pivot operations and write the pivot table to an Excel sheet. My code is like this
`varz = ampl.getVariable('z')

df3 = varz.getValues()
`
when I print df3, this is what I get

index0 index1 index2 | z.val
1 1 1 | 0
1 1 2 | 0
1 1 3 | 0
1 1 4 | 0
1 1 5 | 0
1 1 6 | 0
1 1 7 | 0
1 1 8 | 0
1 1 9 | 0
1 1 10 | 0
1 1 11 | 0
1 1 12 | 0
1 2 1 | 0
1 2 2 | 0

When I do any dataframe attributes like 'to_excel' or 'groupby' or 'columns', I get the error AttributeError: 'DataFrame' object has no attribute .... In summary, I need to save multidimensional variable from an optimization to excel. Any help is greatly appreciated.

df3 = varz.getValues() returns an amplpy dataframe. In order to obtain a pandas dataframe that you can then export to Excel you need to use .toPandas() as follows:

from amplpy import AMPL
ampl = AMPL()
ampl.eval('var z{1..10, 1..10, 1..10};')
df = ampl.getVariable('z').getValues().toPandas()
df.to_excel('var_z.xlsx')

Thank you for the quick response. The conversion to the pandas gives me a dataframe as given below

                     > z.val

(1.0, 1.0, 1.0) 73.0
(1.0, 1.0, 2.0) 51.0
(1.0, 1.0, 3.0) 53.0
(1.0, 1.0, 4.0) 71.0
(1.0, 1.0, 5.0) 72.0
... ...
(8.0, 6.0, 11.0) 10.0
(8.0, 6.0, 12.0) 11.0

In order to the analysis (pivot table) that I need, if the output is produced with indexes (i = 1..8, j = 1..6) and columns ( k =1...12) like the example below. It would be great. How can I create a dataframe like the one below.

      1    2   3   4   5 ...

1 1 73 51 53 71 72..
1 2 0 87 65 34 22 ..
1 3 7 8 9 3 1 ...

df3 = varz.getValues() returns an amplpy dataframe. In order to obtain a pandas dataframe that you can then export to Excel you need to use .toPandas() as follows:

from amplpy import AMPL
ampl = AMPL()
ampl.eval('var z{1..10, 1..10, 1..10};')
df = ampl.getVariable('z').getValues().toPandas()
df.to_excel('var_z.xlsx')
             z.val

(1.0, 1.0, 1.0) 73.0
(1.0, 1.0, 2.0) 51.0
(1.0, 1.0, 3.0) 53.0
(1.0, 1.0, 4.0) 71.0
(1.0, 1.0, 5.0) 72.0
... ...
(8.0, 6.0, 11.0) 10.0
(8.0, 6.0, 12.0) 11.0

You can do that as follows:

from amplpy import AMPL
import pandas as pd
ampl = AMPL()
ampl.eval('var z{1..8, 1..6, 1..12};')
df = ampl.getVariable('z').getValues().toPandas()
df.index = pd.MultiIndex.from_tuples(df.index, names=['i', 'j', 'k'])
df = df['z.val'].unstack()
df.to_excel('var_z.xlsx')
print(df)
k        1.0   2.0   3.0   4.0   5.0   6.0   7.0   8.0   9.0   10.0  11.0  12.0
i   j
1.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
2.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
3.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
4.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
5.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
6.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
7.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
8.0 1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    2.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    4.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    5.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
    6.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0

Thank you so much. It worked for me.