converting dataframe with label to array in julia

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.)


An update on the convert method, now, convert(::Type{Array}, df::AbstractDataFrame) is deprecated in favor of:

using DataFrames
convert(Matrix, df)

Which is equivalent to Matrix(df)


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

Tags:

Julia