19

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 Answers 5

28

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
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! :-) Happy to help. One note: If you have Null / NA values, you can give convert a third argument to replace them (as they are not allowed in an Array), e.g. convert(Array, iris[1:3], 0.).
With Julia 1.0, it now prompts to change it to convert(Matrix, instead of convert(Array,.
10

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

Comments

8

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)

Comments

6

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.