我正在尝试减少 DataFrames
的数组我已经进入一个DataFrame
通过连接每个 DataFrame
中的公共(public)列.
这是我迄今为止尝试过的示例:
using DataFrames
samples=[DataFrame(x=[1,2,3],y=[1,2,3]),DataFrame(x=[4,5,6],y=[4,5,6]),DataFrame(x=[7,8,9],y=[7,8,9])]
hcat(select.(samples,:y)[1],select.(samples,:y)[2],select.(samples,:y)[3],makeunique=true) #this works
hcat(select.(samples,:y),makeunique=true) #this doesn't
前一个解决方案的问题是 samples
数组可以是任意长,具体取决于数据。
看来makeunique
仅为 DataFrame
定义方法;我需要定义一个新的 DataFrames
hcat
方法?或者我可以以某种方式“输入”每个 DataFrame
这样hcat
将其解释为 DataFrames
的列表?
最佳答案
The DataFrames
package defines an hcat
function它采用可变数量的 DataFrame 对象。您所要做的就是使用 ...
operator 解压 select.
返回的 Array{DataFrame,2}
,像这样:
julia> hcat(select.(samples, :y)..., makeunique=true)
3×3 DataFrame
│ Row │ y │ y_1 │ y_2 │
│ │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┤
│ 1 │ 1 │ 4 │ 7 │
│ 2 │ 2 │ 5 │ 8 │
│ 3 │ 3 │ 6 │ 9 │
关于dataframe - 如何在 Julia 中连接 DataFrame 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62567882/