How to add an array from JuMP values to a colum in a dataframe

441 views Asked by At

I have this array and require to create a dataframe based on that

getvalue.(W)
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, 0:6
And data, a 7-element Array{Float64,1}:
 80.0
 65.0
 65.0
 65.0
 65.0
 65.0
 65.0

I get this error when trying

df=DataFrame(W=getvalue.(W)[1:6])

KeyError: key 1:6 not found
1

There are 1 answers

0
Przemyslaw Szufel On BEST ANSWER

You need to do collect on the DenseAxisArray.

Recreating your data:

julia> using JuMP, DataFrames

julia> vv = JuMP.Containers.DenseAxisArray([80.,65.,65.,65.,65.,65.,65.], 0:6)
1-dimensional DenseAxisArray{Float64,1,...} with index sets:
    Dimension 1, 0:6
And data, a 7-element Array{Float64,1}:
 80.0
 65.0
 65.0
 65.0
 65.0
 65.0
 65.0

Putting the data to a DataFrame:

julia> DataFrame(vv=collect(vv)[1:6])
6×1 DataFrame
│ Row │ vv      │
│     │ Float64 │
├─────┼─────────┤
│ 1   │ 80.0    │
│ 2   │ 65.0    │
│ 3   │ 65.0    │
│ 4   │ 65.0    │
│ 5   │ 65.0    │
│ 6   │ 65.0    │

If your vv is big you might also save time and memory on collecting it with a slightly longer code (the result will be the same):

DataFrame(vv=[vv[CartesianIndex(i)] for i in 1:6])