# How to export Julia data to a .CSV

**URL:** <https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 31, 2019, 1:05pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535 "2019-10-31T13:05:02Z")\
**Posts on this page:** 15\
**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:** [October 31, 2019, 1:05pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/1 "2019-10-31T13:05:02Z")

</div>

I created an empty DataFrame and would like to export the data I got with the model solution I compiled for the DataFrame.

I am doing so, and still showing error.

```julia
df = CSV.read("//arquivo.csv")

for i ∈ 1:3
    df = JuMP.value.(N[1])
end

```

This is the error message that is appearing  
 → Filling the remaining columns with `missing ’

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 31, 2019, 1:17pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/2 "2019-10-31T13:17:33Z")

</div>

This code _reads_ a CSV file into `df` (which is where you get the error mesage — it may not be a valid CSV file), and then _overwrites_ this value with `JuMP.value.(N[1])`.

If you want to _export_ data to CSV, you probably need [`CSV.write`](https://juliadata.github.io/CSV.jl/stable/#CSV.write).

---

<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:** [October 31, 2019, 1:40pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/3 "2019-10-31T13:40:59Z")

</div>

I tested `CSV.write` and the error is gone, but I haven’t exported the data to CSV yet

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 31, 2019, 1:46pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/4 "2019-10-31T13:46:28Z")

</div>

I am not very familiar with JuMP, but I am under the impression that `JuMP.value` would return a number.

In what format would you prefer to export it into a CSV file?

Also, if you provided an MWE that produces `N`, it would be easier to help.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<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:** [October 31, 2019, 2:31pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/5 "2019-10-31T14:31:01Z")

</div>

Yes, `JuMP.value` returns the value that was found by the solver.  
I know it’s not in MWE format, but maybe with the full code it gets better.  
I would like the `N` build response to be exported to the CSV I created.

```julia
using JuMP, Cbc, DataFrames, CSV
model = Model(with_optimizer(Cbc.Optimizer))
P = [12;60]
M = [0.25 0.1 0.1; 0.5 0.75 0.4]
D = [36; 22; 15]

@variable(model,N[1:2], lower_bound=0)
@objective(model, Max, sum(N[i]*P[i] for i ∈ 1:2))

Pp= transpose(P)

for i ∈ 1:3
    @constraint(model, Pp*M[2*i-2+1:2*i]<= D[i])
end
print(model)

optimize!(model) #model status 
println("Objective value: ", JuMP.objective_value(model))
println("N = ", JuMP.value.(N))

Result= CSV.write("\\File.csv")

for i ∈ 1:2
    Result = JuMP.value.(N[1])
end 

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 31, 2019, 2:53pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/6 "2019-10-31T14:53:29Z")

</div>

In general, you can’t appned columns to a CSV, only rows. If you are tryign to write rows to a CSV you can do

```julia
CSV.write("filename.csv", df, append=true)

```

I believe that for this to work `df` needs to be a dataframe (or other table) with the same format as what already exists in the CSV. You could for example do `DataFrame(value.(N)')` but this would give you the default column names `:x1, :x2`.

I of course don’t know the context of what you’re doing, but I suggest considering a more appropriate format for numerical data such as [HDF5.jl](https://github.com/JuliaIO/HDF5.jl).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 31, 2019, 3:24pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/7 "2019-10-31T15:24:47Z")

</div>

Thanks for posting a self-contained problem. When I run this, it ends up with no (0) solutions, so I am not sure what you would want to print here.

(Sorry, I don’t use JuMP, so I may be missing something obvious here).

---

<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:** [November 1, 2019, 6:03pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/8 "2019-11-01T18:03:19Z")

</div>

I followed your suggestion, and did not reach my goal.  
With this representation maybe I can better explain what I need. First I created an empty `DataFrame`, just with the field called QtdProduction then I made a `for` so that the `DataFrame` was only filled in the QtdProduction field with the compilation results of my model, but it’s giving error.

```julia
Results = CSV.read ("file.csv")

for i ∈ 1: 3
     Results [1,: QtdAProduction] = JuMP.value. (N [i])
end

CSV.write ("file.csv", Results, append = true)

```

---

<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:** [November 1, 2019, 6:08pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/9 "2019-11-01T18:08:41Z")

</div>

Please post the error when you post example code. it makes it easier to help you.

It seems like you aren’t using your loop variable `i` in your loop. Additionally, `: QtdAProduction` is not valid Julia code. You need `:QtdAProduction` instead.

```julia
Results = CSV.read ("file.csv")

for i ∈ 1: 3
     Results [i,:QtdAProduction] = JuMP.value. (N[i])
end

CSV.write ("file.csv", Results, append = true)

```

---

<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:** [November 1, 2019, 6:56pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/11 "2019-11-01T18:56:17Z")

</div>

: QtdAProduction is the name of the field in CSV that I need to fill in.  
This is the error that appears in the `for`.

ArgumentError: CSV.Column is read-only; to get a mutable vector from `copy (col)` or to make all columns mutable from `CSV.read (file; copycols = true)` or `CSV.File (file) |> DataFrame`

---

<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:** [November 1, 2019, 7:01pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/12 "2019-11-01T19:01:13Z")

</div>

You should make the change suggested by the error message. For speed, `CSV.jl` chooses not to make mutable copies of the vectors in a DataFrame.

Once you make that change, also remember to fix the spaces in your `Symbol` commands. The way you are using spaces in your code is not valid Julia syntax and will cause your code to break.

---

<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:** [November 4, 2019, 7:00pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/14 "2019-11-04T19:00:43Z")

</div>

I made the adjustments and the code was like this:

```julia
println("N = ",JuMP.value.(N))
Results = CSV.read("file.csv";copycols=true)#I would like .CSV were filled with the answer that println()

for i ∈ 1:3
    copy(Results)[i,:QtdAProduzir] = JuMP.value.(N[i])
end

CSV.write("file.csv",Results,append = true)

```

But it is not yet possible to populate .CSV with Julia’s compile result

---

<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:** [November 4, 2019, 7:07pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/15 "2019-11-04T19:07:36Z")

</div>

When you write `copy(Results)` in your loop, you are making a new dataframe that is _not_ `Results`.

Now that you have written `copycols = true` you do not need to copy again. Simply modify `Results` in the loop.

Remember that `copy` creates something totally new. You want to work with the existing data frame `Results`.

---

<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:** [November 4, 2019, 7:31pm UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/17 "2019-11-04T19:31:06Z")

</div>

The code is adding to the .CSV the fields that were already filled, that is, duplicating the information, when it should include the empty fields of the results obtained with `println()`.

```julia
println("N = ",JuMP.value.(N))
Results = CSV.read("file.csv";copycols=true)

for i ∈ 1:3
    copy(JuMP.value.(N[i]))
end

CSV.write("file.csv",Results,append=true)

```

---

<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:** [November 5, 2019, 1:50am UTC](https://discourse.julialang.org/t/how-to-export-julia-data-to-a-csv/30535/18 "2019-11-05T01:50:55Z")

</div>

It’s not clear what your code is trying to do. `copy(JuMP.value.(N[i]))` doesn’t assign assign anything to a variable.

Perhaps you mean

```julia
Results[i,:QtdAProduzir] = JuMP.value.(N[i])

```
