# How can I save this data with CSV?

**URL:** <https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159>\
**Category:** Data\
**Tags:** csv, namedtuple\
**Created:** [September 6, 2020, 9:21pm UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159 "2020-09-06T21:21:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [September 6, 2020, 9:21pm UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159/1 "2020-09-06T21:21:41Z")

</div>

I am confused about the CSV way of writing files.  
My problem is really simple:  
I have two arrays and I want to save them in a .csv file, but I also want the following column names above my data: `Voltage [V]` and `Current [A]` which obviously are strings.

I tried an example with names tuples, but I don’t like that the names are propagated to the .csv as well:

```julia
diode_volt = []
diode_crt = []
append!(diode_volt, 0:0.1:10); 
append!(diode_crt, exp.(diode_volt));
# append header names

```

~~pushfirst!(diode\_volt, “diode voltage [V]”)~~  
~~pushfirst!(diode\_crt, “diode current [A]”)~~  
~~CSV.write(“diode.csv”, (diode\_volt = diode\_volt, diode\_crt = diode\_crt))~~

~~What is the shortest way to solve this?~~

Nevermind, I read the docs more carefully and you can overwrite the header 😅:  
`CSV.write("diode.csv", (diode_volt = diode_volt, diode_crt = diode_crt); header = ["Voltage [V]", "Current [A]"])`

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [September 6, 2020, 10:10pm UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159/2 "2020-09-06T22:10:30Z")

</div>

By default, `CSV.write` uses the namedtuple keys as column names, which is admittedly a bit awkward in your situation due to the spaces in the column names, but nevertheless, it would work to do:

```julia
tbl = NamedTuple{(Symbol("Voltage [V]"), Symbol("Current [A]"))}((
    0:0.1:10,
    exp.(0:0.1:10)
))
CSV.write("diode.csv", tbl)

```

---

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [September 7, 2020, 4:03am UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159/3 "2020-09-07T04:03:22Z")

</div>

You can use a dataframe which makes things somewhat simpler.

```julia
using DataFrames
df = DataFrames.DataFrame( Voltage = diode_volt,Current = diode_crt )

CSV.write("filename.csv", df)

```

Sadly this won’t allow you to add [V] or [A] as ‘[’ and ‘]’ are forbidden in variables names.

---

<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:** [September 7, 2020, 6:03am UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159/4 "2020-09-07T06:03:07Z")

</div>

```julia
julia> using DataFrames

julia> df = DataFrame("Voltage [V]" => rand(5), "Current [A]" => rand(5))
5×2 DataFrame
│ Row │ Voltage [V] │ Current [A] │
│ │ Float64 │ Float64 │
├─────┼─────────────┼─────────────┤
│ 1 │ 0.0793645 │ 0.964186 │
│ 2 │ 0.12372 │ 0.921271 │
│ 3 │ 0.223653 │ 0.485401 │
│ 4 │ 0.151043 │ 0.242418 │
│ 5 │ 0.885049 │ 0.204711 │

```

seems good to me?

---

<div class="post-metadata">

**Author:** ![Codzilla](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@Codzilla](https://discourse.julialang.org/u/Codzilla)\
**Post date:** [September 7, 2020, 6:20am UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159/5 "2020-09-07T06:20:12Z")

</div>

that’s a nice way to it. I was using the keyword arguments but your solution with dic entries allows for arbitrary strings.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [September 7, 2020, 10:53am UTC](https://discourse.julialang.org/t/how-can-i-save-this-data-with-csv/46159/6 "2020-09-07T10:53:46Z")

</div>

Thank you all for your solutions! In the end I used @quinnj’s method because I didn’t want to include DataFrames in the setup for such a simple table.
