# Extracting JuMP results

**URL:** https://discourse.julialang.org/t/extracting-jump-results/51429
**Category:** Optimization (Mathematical)
**Tags:** jump, error, optimization, dataframes
**Created:** [December 7, 2020, 9:09pm UTC](https://discourse.julialang.org/t/extracting-jump-results/51429 "2020-12-07T21:09:27Z")
**Posts on this page:** 1
**Showing post:** 6

<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: [December 10, 2020, 12:49am UTC](https://discourse.julialang.org/t/extracting-jump-results/51429/6 "2020-12-10T00:49:13Z")

</div>

In regards to Question 1., this is the code I use to convert optimisation results to DataFrames:

```julia
"""
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::Symbol=:Value)

    if isempty(var)
        return DataFrame()
    end

    if length(dim_names) == 0
        dim_names = [Symbol("dim$i") for i in 1:length(var.axes)]
    end

    if length(dim_names) != length(var.axes)
        throw(ArgumentError("Length of given name list does not fit the number of variable dimensions"))
    end

    tup_dim = (dim_names...,)

    # With a product over all axis sets of size M, form an Mx1 Array of all indices to the JuMP container `var`
    ind = reshape([collect(k[i] for i in 1:length(dim_names)) for k in Base.Iterators.product(var.axes...)],:,1)

    var_val = value.(var)

    df = DataFrame([merge(NamedTuple{tup_dim}(ind[i]), NamedTuple{(value_col,)}(var_val[(ind[i]...,)...])) for i in 1:length(ind)])

    return df
end

```

---

_[View the full topic](https://discourse.julialang.org/t/extracting-jump-results/51429)._
