# Converting Dense Axis array results into dataframe

**URL:** <https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, dataframes\
**Created:** [July 5, 2022, 3:18pm UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780 "2022-07-05T15:18:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)\
**Post date:** [July 5, 2022, 3:18pm UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/1 "2022-07-05T15:18:04Z")

</div>

Hello,

I have the following two-dimensional dense axis array results extracted from a constraint.

 ![shadow prices](https://global.discourse-cdn.com/julialang/original/3X/6/9/694e4d2d689fd3bfce06af68baac26847a6ace5f.jpeg)

Rows represent time stamps [T] and columns represent nodes [N]. Is there a way of converting the DenseAxisArray into a dataframe with the same dimensions?

Thanks

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 5, 2022, 4:06pm UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/2 "2022-07-05T16:06:26Z")

</div>

Can you provide an MWE? See some advice on asking a question [here](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)\
**Post date:** [July 5, 2022, 4:47pm UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/3 "2022-07-05T16:47:54Z")

</div>

Sure, below is a small example. I have commented out the parts that are not central to the discussion. To elaborate on my earlier query, I am trying to convert the two-dimensional shadow price results obtained from the energy balance constraint (EB\_nodal) into a dataframe.

```julia
#using JuMP, DataFrames, CSV, GLPK 

generators = DataFrame(index = ["OIL_A", "GAS_B", "NUC_C"], 
                        node = ["zonea", "zoneb", "zonec"],
                        cost = [30,10,5], g_max = [50,60,70], 
                        plant_type = ["conventional","conventional","conventional"])
demand = [0 0 50; 0 0 100; 0 0 150]

T = convert(Array{Int64}, 1:3) #Time
N = convert(Array{Int64}, 1:3) #node
L = convert(Array{Int64}, 1:3) 
C = generators.index

locName = Dict(1 => "zonea", 2 => "zoneb", 3 => "zonec")
incidence = [1 -1 0; 1 0 -1; 0 1 -1]

```

```julia
model = Model(GLPK.Optimizer)

#variables
#@variable(model, vGEN[T, c in C]>=0)
#@variable(model, vFLOW[T, L]) 

#Objective function
#@expression(model, DispCost[t in T], sum(vGEN[t, c]*generators[findfirst(==(c), generators.index), :].cost for c in C)) 
#@objective(model, Min, sum(Dispatch_Model[:DispCost])) 

#Constraints

#@constraint(model, cMaxPower[t in T, c in C], vGEN[t,c] <= generators[findfirst(==(c), C), :].g_max)
#@expression(model, convGEN[t in T, n in N], sum(vGEN[t, c]* (locName[n] == generators[findfirst(==(c), C), :node] ? 1.0 : 0.0) for c in C))

@constraint(model,
 EB_nodal[t in T, n in N],
 demand[t,n] == convGEN[t,n]
  - sum(incidence[l, n]*vFLOW[t, l] for l in L)
) #main constraint

```

```julia
optimize!(model)
nodal_pr = shadow_price.(EB_nodal) * -1 #How do i convert a DenseAxisArray results into a dataframe

```

Shadow price results from EB\_nodal constraint

```julia
2-dimensional DenseAxisArray{Float64,2,...} with index sets:
    Dimension 1, [1, 2, 3]
    Dimension 2, [1, 2, 3]
And data, a 3×3 Matrix{Float64}:
  5.0 5.0 5.0
 10.0 10.0 10.0
 30.0 30.0 30.0

```

Thanks!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 5, 2022, 11:04pm UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/4 "2022-07-05T23:04:25Z")

</div>

There’s no special support. You’d have to construct the dataframe manually. Potentially something like this:

```julia
t_list = [t for t in T for _ in N]
n_list = [n for _ in T for n in N]
x_list = [nodal_pr[t, n] for (t, n) in zip(t_list, n_list)]
DataFrame(t = t_list, n = n_list, shadow_price = x_list)

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [July 6, 2022, 2:50am UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/5 "2022-07-06T02:50:47Z")

</div>

This hopefully still works:

> [@Extracting JuMP results](https://discourse.julialang.org/t/extracting-jump-results/51429/6):
>
> In regards to Question 1., this is the code I use to convert optimisation results to DataFrames: """ Returns a `DataFrame` with the values of the variables from the JuMP container `var`. The column names of the `DataFrame` can be specified for the indexing columns in `dim_names`, and the name of the data value column by a Symbol `value_col` e.g. :Value """ function convert\_jump\_container\_to\_df(var::JuMP.Containers.DenseAxisArray; dim\_names::Vector{Symbol}=Vector{Symbol}(), value\_col::Sy…

You can then do an `DataFrames.unstack` to pivot the table.

---

<div class="post-metadata">

**Author:** ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)\
**Post date:** [July 6, 2022, 8:20am UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/6 "2022-07-06T08:20:12Z")

</div>

Thanks @jd-foster, this function works

---

<div class="post-metadata">

**Author:** ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)\
**Post date:** [July 6, 2022, 8:23am UTC](https://discourse.julialang.org/t/converting-dense-axis-array-results-into-dataframe/83780/7 "2022-07-06T08:23:17Z")

</div>

Thanks @odow. This approach works as well. Following @jd-fosters suggestion, i added the unstack dataframe line

```julia
t_list = [t for t in T for _ in N]
n_list = [n for _ in T for n in N]
x_list = [nodal_pr[t, n] for (t, n) in zip(t_list, n_list)]
nodal_price = DataFrame(t = t_list, n = n_list, shadow_price = x_list)
new_nodal_price = unstack(nodal_price, :t, :n, :shadow_price)

```

Thanks!
