Help with CSV and Dataframe

Hello everybody

I’m still learning to program in julia and I came across this doubt. How to save data in a DataFrame using the for command?
Whenever I try to compile the last for it displays this error message: MethodError: no method matching getindex!

Thanks

using JuMP, MathOptInterface, GLPK, GLPKMathProgInterface, CSV, DataFrames
ModeloD = Model(solver = GLPKSolverMIP())

a = 4
t = 4
w = 2

A = 1:a
T = 1:t
W = 1:w

NE = [14, 53]
WH = [2489 1447 3513 20914; 11073 5414 1523 16282]
CR = [11, 12]
CO = [50, 51]

jump_vars = Dict{Symbol, Any}()
jump_vars[:c] = @variable(ModeloD,c[A],Bin)
jump_vars[:r] = @variable(ModeloD,r[A,T] <=0)
jump_vars[:wo] = @variable(ModeloD,wo[W,T]>=0)
jump_vars[:wr] = @variable(ModeloD,wr[W,T]<=0)
jump_vars[:er] = @variable(ModeloD,er[W,T]<=0)
jump_vars[:eo] = @variable(ModeloD,eo[W,T]<=0)
jump_vars[:s] = @variable(ModeloD,s[A,T]<=0)
jump_vars[:x] = @variable(ModeloD,x[A,T]<=0)

@objective(ModeloD,Min,sum(CR[w]*(wr[w,t]+wo[w,t])+CO[w]*wo[w,t] for w in W, t in T))

for w = 1:w, t= 1:t
    n= @constraint(ModeloD, er[w,t]<= NE[w])
    #println(n)
end


for w = 1:w,t= 1:t
    h = @constraint(ModeloD,eo[w,t]<=NE[w])
    #println(h)
end


for w = 1:w,t= 1:t
    Γ§ = @constraint(ModeloD,wr[w,t]+wo[w,t]==WH[w,t])
    #println(Γ§)
end


print(ModeloD)
status = solve(ModeloD)
println("objective value:  ", JuMP.getobjectivevalue(ModeloD))

Result = CSV.read("file.csv", DataFrame)#DataFrame creating to receive variables

#Here all variables are read
for (current_name, current_var) in jump_vars
    println("$current_name =  ", JuMP.getvalue(current_var))

end

#Here all variables should be exported to the DataFrame 
#:resultvariable is the name of the DataFrame field that I need to fill out.
for jump_vars in CSV.File("file.csv")
    Result[c,:resultvariable] = JuMP.getvalue(jump_vars[c])
end
1 Like

I’ve moved this to Data because it isn’t related to JuMP.

Again, please read PSA: make it easier to help you and provide the full text of the error you encounter.

In your code

for jump_vars in CSV.File("file.csv")
    Result[c,:resultvariable] = JuMP.getvalue(jump_vars[c])
end

c is a JuMP variable, so it doesn’t makes sense to index a data frame by a JuMP variable.

1 Like

As Oscar says, it would be great if you again consider the PSA to understand what a minimum working example is - this massively helps others to help you, and often the attempt to boil your problem down to the minimum code required to reproduce your error lets you find the error yourself!

Just to quickly summarize the PSA, your minimum working example should be:

  • Minimal, meaning you should restrict yourself to the fewest lines of code that reproduce the error you’re running into. If your question is, as it seems to be here, how do I save data in a DataFrame in a for loop, then it is unlikely that a minimal example would include JuMP, MathOptInterface, GLPK or really any other packages than just DataFrames
  • Working, meaning that it can be copy-pasted by other users onto their local machines and will run without errors (except for the one you’re interested in solving of course) - this mainly means it can’t include any undefined variables, or rely on data saved locally on your machine.

I’m still confused about what your code does and cannot run it myself as it relies on "file.csv", but I’ll give you a minimal working example of how to write values contained in a vector into the column of a DataFrame in a loop:

julia> using DataFrames

julia> jump_results = [5.0, 8.0, 3.0];

julia> results = DataFrame(model_run = 1:3, resultvariable = zeros(3))
3Γ—2 DataFrame
 Row β”‚ model_run  resultvariable 
     β”‚ Int64      Float64        
─────┼───────────────────────────
   1 β”‚         1             0.0
   2 β”‚         2             0.0
   3 β”‚         3             0.0

julia> for (row, result) ∈ zip(eachrow(results), jump_results)
           row.resultvariable = result
       end

julia> results
3Γ—2 DataFrame
 Row β”‚ model_run  resultvariable 
     β”‚ Int64      Float64        
─────┼───────────────────────────
   1 β”‚         1             5.0
   2 β”‚         2             8.0
   3 β”‚         3             3.0

Note however that when you just want to add a column of numbers to a DataFrame, the loop is unnecessary as you can just create a new column like so:

julia> results[!, :resultvariable_again] = jump_results
3-element Vector{Float64}:
 5.0
 8.0
 3.0

julia> results
3Γ—3 DataFrame
 Row β”‚ model_run  resultvariable  resultvariable_again 
     β”‚ Int64      Float64         Float64              
─────┼─────────────────────────────────────────────────
   1 β”‚         1             5.0                   5.0
   2 β”‚         2             8.0                   8.0
   3 β”‚         3             3.0                   3.0
1 Like