Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 223
numbersguy132 Avatar asked Oct 24 '25 15:10

numbersguy132


1 Answers

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.

like image 175
Bogumił Kamiński Avatar answered Oct 26 '25 13:10

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!