19
votes

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 ....
*  | *  | *  | *  ....
.  | .  | .  | .  ....
.  | .  | .  | .  ....
.  | .  | .  | .  ....
5

5 Answers

26
votes

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
10
votes

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

7
votes

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)

4
votes

This doesn't work in Julia 0.7 and above. Instead, try Matrix(df) and check out the tutorial here.

1
votes

The former solution does not work try Matrix(df,[:,1:3])