# How to write a specific column name in CSV.write function?

**URL:** <https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 21, 2021, 7:57pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176 "2021-10-21T19:57:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 21, 2021, 7:57pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/1 "2021-10-21T19:57:47Z")

</div>

I have an empty `csv` file named “Output”. I want to to show the values of `V` under a specific column name (in my case `V_S1` as will be shown by the `println()`). However, I am having the below error. Any idea to handle this issue?

```julia
using CSV, DataFrames
timeSim = 0:3;
V=[1 2 3];
List=["S1" "S2"];
println("V_$(List[1])")
CSV.write("C:\\Users\\...\\Output.csv", DataFrame( time=timeSim, println("V_$(List[1])")=V[:]))
ERROR: LoadError: syntax: incomplete: premature end of input
Stacktrace:

```

---

<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:** [October 21, 2021, 8:00pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/2 "2021-10-21T20:00:44Z")

</div>

You are missing a closing parentheses in your code snippet. Maybe that’s your problem?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 21, 2021, 8:03pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/3 "2021-10-21T20:03:48Z")

</div>

Yes, thanks for your correct. I have updated the code but now there is another error:

```julia
ERROR: LoadError: syntax: invalid keyword argument name "println("$("V_")$(List[1])")" around

```

---

<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:** [October 21, 2021, 8:05pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/4 "2021-10-21T20:05:34Z")

</div>

Your errors are still unrelated to the behavior of `CSV.write`. Keep trying on fixing syntactical issues (making sure your code is valid Julia code), so that we can better understand your core problem.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 21, 2021, 8:10pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/5 "2021-10-21T20:10:43Z")

</div>

Yes, actually my problem is under syntactical issues because I dont know how to make it. I want eventually to have the `V` vector to be shown under a column named `V_S1` as in the below: ![1](https://global.discourse-cdn.com/julialang/original/3X/b/c/bcd034d0bc086029b5cd350dcd68596c6c182b82.jpeg)

---

<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:** [October 21, 2021, 8:11pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/6 "2021-10-21T20:11:44Z")

</div>

Have you used [ClipData.jl](https://github.com/pdeffebach/ClipData.jl)? It is useful for getting things to and from excel spreadsheets.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 21, 2021, 8:20pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/7 "2021-10-21T20:20:18Z")

</div>

No I didn’t and I will have a look. However, I am trying to rephrase my question as follows.

I have an empty `csv` file named “Output”. I want to name a column as `V_S1`, in which `S1` is a the first string element of vector `List`.  
 ![1](https://global.discourse-cdn.com/julialang/original/3X/b/c/bcd034d0bc086029b5cd350dcd68596c6c182b82.jpeg)

How can I do that rather than writing as in the comment?

```julia
using CSV, DataFrames
timeSim = 1:3;
V=[1 2 3];
List=["S1" "S2"];
#CSV.write("C:\\Users\\...\\Output.csv", DataFrame( time=timeSim, V_S1=V[:]))
CSV.write("C:\\Users\\...\\Output.csv", DataFrame( time=timeSim, println("V_$(List[1])")=V[:]))

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [October 21, 2021, 8:22pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/8 "2021-10-21T20:22:38Z")

</div>

Symbol instead of println?

---

<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:** [October 21, 2021, 8:25pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/9 "2021-10-21T20:25:10Z")

</div>

You aren’t constructing your DataFrame correctly, and it’s causing errors.

It will help to have each line be very simple. Construct the data frame on one line, write it to a CSV on another.

```julia
df = DataFrame("time" => timeSim, "V_$(List[1])" => V[:])
CSV.write("C:\\Users\\...\\Output.csv", df)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 21, 2021, 8:30pm UTC](https://discourse.julialang.org/t/how-to-write-a-specific-column-name-in-csv-write-function/70176/10 "2021-10-21T20:30:34Z")

</div>

Thank you very much, I got it.
