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 ....
* | * | * | * ....
. | . | . | . ....
. | . | . | . ....
. | . | . | . ....
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
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With