gpt4 book ai didi

dataframe - Julia 中 DataFrame 的两列或多列的矢量化连接

转载 作者:行者123 更新时间:2023-12-05 04:07:43 24 4
gpt4 key购买 nike

我有一个 Julia DataFrame,其中包含多个 StringInt 列。我想以矢量化方式将它们水平粘合在一起以生成一列。在 R 中,我会使用 paste。这在 Julia 中可能吗?

所需的输出不是 hcatvcat 操作的输出,例如 these .目标是创建一个新的字符串列,其中包含 "x1[i]:x2[i]" 行,其中 x1[i]x2[ i]DataFrame 对象的 x1x2 列中的相应行元素。

Julia 示例:

# tested in Julia v0.5.0 and v0.6.2
# example data frame
y = DataFrame(x1 = [1,2,3], x2 = ["A","B","C"])

# goal: make column ["1:A"; "2:B", "3:C"]
# desired output format for one row
join( [ y[1,:x1], y[1,:x2] ], ":" ) # > "1:A"

# doesn't work with vectors, makes one long string
# (0.5) > "[1,2,3]:String[\"A\",\"B\",\"C\"]"
# (0.6) > "Any[1, 2, 3]:Any[\"A\", \"B\", \"C\"]"
join([y[:,:x1], y[:,:x2]], ":")

# default broadcast operation doesn't work either
# (0.5) > ERROR: MethodError: no method matching size(::String)
# (0.6) > 2-element Array{String,1}:
# "1:2:3"
# "A:B:C"
join.([y[:,:x1], y[:,:x2]], ":")

R 示例

# same data structure as before
y = data.frame(x1 = c(1:3), x2 = c("A", "B", "C"))

# desired output format with 'paste'
paste(y$x1, y$x2, sep = ":") # > "1:A" "2:B" "3:C"

最佳答案

可能的替代方案是:

  1. ["$(r[:x1]):$(r[:x2])"for r in eachrow(y)]

  2. [join(Array(r),":") for r in eachrow(y)]

  3. mapslices(x->join(x,":"),(Array(y)),2)

  4. map(x->join(x,":"),zip(y[:x1],string.(y[:x2])))

  5. [string(y[:x1][i])*":"*string(y[:x2][i]) for i=1:nrow(y)]

它们在性能方面并不完全相同(选项 5 最快但更具体)。

关于dataframe - Julia 中 DataFrame 的两列或多列的矢量化连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48392483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com