# Help with CSV and Dataframe

**URL:** <https://discourse.julialang.org/t/help-with-csv-and-dataframe/53930>\
**Category:** Data\
**Tags:** question, package\
**Created:** [January 25, 2021, 8:47pm UTC](https://discourse.julialang.org/t/help-with-csv-and-dataframe/53930 "2021-01-25T20:47:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)\
**Post date:** [January 25, 2021, 8:47pm UTC](https://discourse.julialang.org/t/help-with-csv-and-dataframe/53930/1 "2021-01-25T20:47:27Z")

</div>

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

```julia
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

```

---

<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:** [January 25, 2021, 9:15pm UTC](https://discourse.julialang.org/t/help-with-csv-and-dataframe/53930/2 "2021-01-25T21:15:07Z")

</div>

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

Again, please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) and provide the full text of the error you encounter.

In your code

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 26, 2021, 7:27am UTC](https://discourse.julialang.org/t/help-with-csv-and-dataframe/53930/3 "2021-01-26T07:27:13Z")

</div>

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
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
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

```
