Dataframes.jl左联接等效于数组

时间:2018-06-27 00:35:49

标签: julia

是否有一个可以离开连接数组的软件包,就像dataframes.jl离开连接数据帧的方式一样?

1 个答案:

答案 0 :(得分:1)

如果您有复杂的记录作为数组,则最好只使用DataFrames.jl。将数组放入数据帧中,然后加入,然后使用Array构造函数提取。

如果您确切知道左联接的逻辑将永远是什么,那么您应该能够有效地使用列表推导。但是,在通用左联接的逻辑级别上,最好使用数据帧。

julia> using DataFrames
julia> names = DataFrame(ID = [20, 40], Name = ["John Doe", "Jane Doe"])
2×2 DataFrames.DataFrame
│ Row │ ID │ Name     │
├─────┼────┼──────────┤
│ 1   │ 20 │ John Doe │
│ 2   │ 40 │ Jane Doe │

julia> jobs = DataFrame(ID = [20, 40], Job = ["Lawyer", "Doctor"])
2×2 DataFrames.DataFrame
│ Row │ ID │ Job    │
├─────┼────┼────────┤
│ 1   │ 20 │ Lawyer │
│ 2   │ 40 │ Doctor │

julia> arr = Array(join(names, jobs, on = :ID, kind = :left))
2×3 Array{Any,2}:
20  "John Doe"  "Lawyer"
40  "Jane Doe"  "Doctor"

..或

julia> n = Array(names)
2×2 Array{Any,2}:
20  "John Doe"
40  "Jane Doe"

julia> j = Array(jobs)
2×2 Array{Any,2}:
20  "Lawyer"
40  "Doctor"

julia> [ [n[i,2], j[i,2]] for i in 1:size(n)[1] if n[i,1] == j[i,1]]
2-element Array{Array{String,1},1}:
String["John Doe", "Lawyer"]
String["Jane Doe", "Doctor"]