Home > Software design >  Julia dataframe write to csv in plain format
Julia dataframe write to csv in plain format

Time:01-05

Trying to write variables into a database and save as csv, I found out, that the simple variables like x = 1.2345 are written in this format: Pair{Any, Any}("x", 1.234) into the csv-file by default.

In some examples (like https://www.geeksforgeeks.org/working-with-csv-files-in-julia/) the data are written without the type information.

Here is my code:

using DataFrames
using CSV   

function pushDbVals!(df,names,vals)
    if (length(names)!=length(vals))
       error("length(names)!=length(vals): ", length(names),"!=", length(vals))
    end
    row = []
    for d in vals 
        push!(row,d)
    end
    push!(df,row)
    
end


x = 1.234

dbNames = ["x"]
vals = Dict()
vals["x"] = x

println("x: ", x)

df = DataFrame([ name =>[] for name in dbNames])
pushDbVals!(df,dbNames,vals)
CSV.write("test.csv", df)

CodePudding user response:

The reason is that your data frame after the operation does not store floats but pairs. See:

julia> df
1×1 DataFrame
 Row │ x
     │ Any
─────┼────────────────────────────
   1 │ Pair{Any, Any}("x", 1.234)

You most likely wanted the following instead:

julia> df = DataFrame([ name =>[] for name in dbNames])
0×1 DataFrame
 Row │ x
     │ Any
─────┴─────

julia> push!(df, vals)
1×1 DataFrame
 Row │ x
     │ Any
─────┼───────
   1 │ 1.234

as you can see DataFrames.jl already supports pushing rows to a data frame you most likely want. (please check cols and promote kwargs of push! for DataFrame as they allow you to fine-tune the behavior of push! to your needs)

  • Related