dataframe - 如何在 Julia 中连接 DataFrame 数组?

标签 dataframe julia

我正在尝试减少 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/

相关文章:

r - 如何在R中的数据帧的一列中对具有相同值的行进行配对

julia - 如何在 Julia 中快速解决中等规模的 QP 公式?

arrays - 如何在 Julia 1.6.0 中使用 NamedTuples 初始化数组?

pandas - 使用 pandas 中其他列的值名称创建新列

python - 如何使用时间点计算 Pandas 中的累积 groupby 计数?

python - 来自一系列数据帧的 Pandas 多重索引

parallel-processing - julia 任务中的变量范围

Julia:如何将非均匀间隔的二维数据插入到网格上?

julia - DynamicalSystems 初始条件不工作的轨道图

Python - 使用 Pandas 和 openpyxl 修改现有的 excel