Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting dataframe with label to array in julia

Tags:

julia

I have data frame with header in Julia , but I need to convert this to array for some filtering , there is some similar post that people suggest using :

iris[:, 1:3]

to get array from dataframe but this method wont work on dataframe with header , any suggestion what should I do ?

dataframe format :

FP | C1 | Cz | C2 ....
*  | *  | *  | *  ....
.  | .  | .  | .  ....
.  | .  | .  | .  ....
.  | .  | .  | .  ....
like image 768
MJ Sameri Avatar asked Mar 21 '17 19:03

MJ Sameri


2 Answers

Have you tried convert(Matrix, iris[:,1:3])? e.g.

julia> using DataFrames

julia> df = DataFrame(a = 1:4, b = 1:4, c = randn(4), d = randn(4))
4×4 DataFrame
│ Row │ a     │ b     │ c        │ d          │
│     │ Int64 │ Int64 │ Float64  │ Float64    │
├─────┼───────┼───────┼──────────┼────────────┤
│ 1   │ 1     │ 1     │ 1.72172  │ -0.377729  │
│ 2   │ 2     │ 2     │ 0.206415 │ -0.266014  │
│ 3   │ 3     │ 3     │ 1.03785  │ -0.0317582 │
│ 4   │ 4     │ 4     │ 0.632473 │ -0.409014  │

julia> convert(Matrix, df[:,1:3])
4×3 Array{Float64,2}:
 1.0  1.0  1.72172
 2.0  2.0  0.206415
 3.0  3.0  1.03785
 4.0  4.0  0.632473
like image 164
Michael K. Borregaard Avatar answered Jan 03 '23 04:01

Michael K. Borregaard


The accepted answer does a good job of answering the question as stated.

If your only reason for wanting to convert the DataFrame to an Array is to filter it, though, it may be worth looking into the methods available for filtering DataFrame objects directly. See, for some examples, https://dataframesjl.readthedocs.io/en/latest/subsets.html and https://dataframesjl.readthedocs.io/en/latest/split_apply_combine.html.

(Sorry in advance if this is better suited for a comment than an answer—not enough reputation to comment on here yet.)

like image 44
Julian Wolf Avatar answered Jan 03 '23 05:01

Julian Wolf