jupyter-notebookjuliajulia-dataframe

Reading Strings as Vectors Julia


I currently have a Julia dataframe of the form

A B
"[1,2]" "[3,4]"

and would like to make it of the form

A1 A2 B1 B2
1 2 3 4

or of the form (where the vectors are no longer strings).

| A | B | |---|---| |[1,2]|[3,4]| is there any way to do this? I have already looked at a few posts where people try to convert vectors of the form ["1", "2"] to the form [1,2] but nothing along the lines of what I have.

Thanks for the help.


Solution

  • Here is an example way how you can do it:

    julia> using DataFrames
    
    julia> df = DataFrame(A="[1,2]", B="[3,4]")
    1×2 DataFrame
     Row │ A       B
         │ String  String
    ─────┼────────────────
       1 │ [1,2]   [3,4]
    
    julia> select(df, [:A, :B] .=>
                      ByRow(x -> parse.(Int, split(chop(x, head=1, tail=1), ','))) .=>
                      [[:A1, :A2], [:B1, :B2]])
    1×4 DataFrame
     Row │ A1     A2     B1     B2
         │ Int64  Int64  Int64  Int64
    ─────┼────────────────────────────
       1 │     1      2      3      4
    

    If something requires an explanation please ask in the comment.