F612
F612

Reputation: 566

Julia Create Column name from a variable when writing to a csv file

I'm trying to write a column name which has a variable. The other thing Im trying to get is the append function column-wise.

CSV.write("File_Name.csv",(;"column$i"::String=sort(val)),append=true)

where i is generated in for loop. Also, how to append in next column? e.g. if there are 2 columns

column 1 | column2 |

then whats the way to add new column next to them as column 3?

Upvotes: 0

Views: 551

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

That's a pretty unusal way of going about things, and the right answer is probably to change your approach more fundamentally - probably building up the table in a reasonable format in your code, and then writing out when you have it.

Fundamentally I believe you can't append columns to csv files, the append keyword works on a row-basis. You can transpose whatever you're reading in though, so you could just append your columns as rows and then read in transposed.

An example where there are five columns to be written out, each of which holds ten strings sorted alphabetically:

julia> using CSV, DataFrames, Random

julia> for i ∈ 1:5
           val = sort([randstring(3) for _ ∈ 1:10])
           CSV.write("test.csv", DataFrame(permutedims(val), :auto), append = true)
       end

julia> CSV.read("test.csv", DataFrame; transpose = true, header = false)
10×5 DataFrame
 Row │ Column1  Column2  Column3  Column4  Column5 
     │ String3  String3  String3  String3  String3 
─────┼─────────────────────────────────────────────
   1 │ 98g      AW6      02L      0HL      10n
   2 │ Anq      Bgv      8JB      0jf      8RH
   3 │ FCL      D2n      8Z1      2wd      O5N
   4 │ SUf      QwW      FhK      7jk      QfP
   5 │ ewW      Sd2      Jxw      EEv      XgW
   6 │ mG9      Thi      Tbk      Lx1      cqi
   7 │ mm5      jI6      WI8      QsI      lbm
   8 │ nEa      ou5      bRs      S3o      sIF
   9 │ u2w      rnz      hb1      TPh      tJD
  10 │ zKW      tZh      x2J      bJb      tPn

Here val is a column in the final table - 10 random strings of length 3, sorted alphabetically. I then put this vector in a DataFrame transposed, which means instead of one column of lenght 10, I get a table with 10 columns and one row:

julia> DataFrame(permutedims(val), :auto)
1×10 DataFrame
 Row │ x1      x2      x3      x4      x5      x6      x7      x8      x9      x10    
     │ String  String  String  String  String  String  String  String  String  String 
─────┼────────────────────────────────────────────────────────────────────────────────
   1 │ 3vh     J4M     JDu     Y2P     Zcb     dLA     dTy     oU6     rhG     tN2

the column names x1 to x10 come from the :auto kwarg, but are irrelevant here because CSV.write with append = true will ignore the header anyway.

Doing this in a loop I therefore end up with a 5 row, 10 column csv file. Reading this in with transpose = true will give me a 10x5 table, and header = false means that CSV will just assign Column1...Column5 as column names.

Upvotes: 1

Related Questions