我想知道 Julia DataFrames 是否有办法一次性加入多个数据帧,
using DataFrames
employer = DataFrame(
ID = Array{Int64}([01,02,03,04,05,09,11,20]),
name = Array{String}(["Matthews","Daniella", "Kofi", "Vladmir", "Jean", "James", "Ayo", "Bill"])
)
salary = DataFrame(
ID = Array{Int64}([01,02,03,04,05,06,08,23]),
amount = Array{Int64}([2050,3000,3500,3500,2500,3400,2700,4500])
)
hours = DataFrame(
ID = Array{Int64}([01,02,03,04,05,08,09,23]),
time = Array{Int64}([40,40,40,40,40,38,45,50])
)
# I tried adding them in an array but ofcoures that results in an error
empSalHrs = innerjoin([employer,salary,hours], on = :ID)
# In python you can achieve this using
import pandas as pd
from functools import reduce
df = reduce(lambda l,r : pd.merge(l,r, on = "ID"), [employer, salary, hours])
Run Code Online (Sandbox Code Playgroud)
在 julia 中是否有类似的方法可以做到这一点?
你快到了。因为它是写在DataFrames.jl 手册中的,你只需要传递一个以上的数据帧作为参数。
using DataFrames
employer = DataFrame(
ID = [01,02,03,04,05,09,11,20],
name = ["Matthews","Daniella", "Kofi", "Vladmir", "Jean", "James", "Ayo", "Bill"])
salary = DataFrame(
ID = [01,02,03,04,05,06,08,23],
amount = [2050,3000,3500,3500,2500,3400,2700,4500])
hours = DataFrame(
ID = [01,02,03,04,05,08,09,23],
time = [40,40,40,40,40,38,45,50]
)
empSalHrs = innerjoin(employer,salary,hours, on = :ID)
Run Code Online (Sandbox Code Playgroud)
如果出于某种原因您需要将数据帧放在 a 中Vector,则可以使用拆分来实现相同的结果
empSalHrs = innerjoin([employer,salary,hours]..., on = :ID)
Run Code Online (Sandbox Code Playgroud)
另外,请注意,我稍微更改了数据框的定义。由于Array{Int}是抽象类型,因此不应将其用于变量声明,因为它对性能不利。在这种特殊情况下,这可能并不重要,但最好从一开始就养成良好的习惯。而不是Array{Int}一个可以使用
Array{Int, 1}([1, 2, 3, 4])Vector{Int}([1, 2, 3, 4])Int[1, 2, 3][1, 2, 3]最后一个是合法的,因为在这个简单的场景中,Julia 可以自己推断容器的类型。