在julia中将带有标签的数据帧转换为数组

时间:2017-03-21 19:55:33

标签: julia

我在Julia中有带头的数据框,但我需要将其转换为数组进行一些过滤,有一些类似的帖子,人们建议 使用:

iris[:, 1:3]

从数据帧中获取数组,但此方法不适用于带有标题的数据框,任何建议我该怎么办?

数据框格式:

FP | C1 | Cz | C2 ....
*  | *  | *  | *  ....
.  | .  | .  | .  ....
.  | .  | .  | .  ....
.  | .  | .  | .  ....

5 个答案:

答案 0 :(得分:16)

你试过convert(Array, 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 DataFrames.DataFrame
│ Row │ a │ b │ c         │ d         │
├─────┼───┼───┼───────────┼───────────┤
│ 1   │ 1 │ 1 │ 0.192261  │ -0.613842 │
│ 2   │ 2 │ 2 │ -0.964262 │ 0.951377  │
│ 3   │ 3 │ 3 │ -0.222804 │ 0.357736  │
│ 4   │ 4 │ 4 │ -0.43415  │ 0.501033  │

julia> convert(Array, df[:,1:3])
4×3 Array{Real,2}:
 1  1   0.192261
 2  2  -0.964262
 3  3  -0.222804
 4  4  -0.43415 

答案 1 :(得分:7)

接受的答案很好地回答了所述的问题。

如果您想要将DataFrame转换为数组的唯一原因是过滤它,那么可能值得研究可用于直接过滤DataFrame对象的方法。有关示例,请参阅https://dataframesjl.readthedocs.io/en/latest/subsets.htmlhttps://dataframesjl.readthedocs.io/en/latest/split_apply_combine.html

(如果这个评论更适合评论而不是答案,请提前抱歉 - 还没有足够的声誉在这里发表评论。)

答案 2 :(得分:1)

现在不推荐使用convert方法的更新,而推荐使用:

convert(::Type{Array}, df::AbstractDataFrame)

相当于using DataFrames convert(Matrix, df)

答案 3 :(得分:1)

以前的解决方案不起作用试试 Matrix(df,[:,1:3])

答案 4 :(得分:0)

这在Julia 0.7及更高版本中不起作用。相反,请尝试Matrix(df)并查看教程here

相关问题