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

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?

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:

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

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

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

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.

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

Have you used ClipData.jl? It is useful for getting things to and from excel spreadsheets.

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

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

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[:]))

Symbol instead of println?

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.

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

Thank you very much, I got it.