首页 > 解决方案 > Julia:通过保留附加变量的多个值折叠 DataFrame

问题描述

我有一些具有重复字段的数据,但我想加入的单个字段除外。在数据中,除了report应该在每一天和每家公司保持不变。公司可以在同一天提交多份报告。

我可以使用以下代码加入,但我丢失了不在我的by函数中的变量。有什么建议么?

模拟数据

using DataFrames

# Number of observations
n = 100
words = split("the wigdet drop air flat fall fling flap freeze flop tool fox", " ")
df = DataFrame(day = cumsum(rand(0:1, n)), company = rand(0:3, n), 
  report = [join(rand(words, rand(1:5, 1)[1]), " ") for i in 1:n])

x = df[:, [:day, :company]]

# Number of variables which are identical for each day/company.
nv = 100
for i in 1:nv
    df[:, Symbol("v" * string(i))] = ""
end

for i in 1:size(x, 1),j in 1:nv
    df[(df.day .== x[i,1]) .& (df.company .== x[i,2]), Symbol("v" * string(j))] = 
    join(rand('a':'z', 3), "")
end

折叠数据

outdf = by(df, [:company, :day]) do sub
  t = DataFrame(fullreport = join(sub.report, "\n(Joined)\n"))
end

标签: dataframejulia

解决方案


以下是您的数据准备代码中的一些小调整:

using DataFrames

# Number of observations
n = 100
words = split("the wigdet drop air flat fall fling flap freeze flop tool fox", " ")
df = DataFrame(day = cumsum(rand(0:1, n)), company = rand(0:3, n), 
  report = [join(rand(words, rand(1:5, 1)[1]), " ") for i in 1:n])

x = df[:, [:day, :company]]

# Number of variables which are identical for each day/company.
nv = 100
for i in 1:nv
    df[:, Symbol("v", i)] .= ""
end

for i in 1:size(x, 1), j in 1:nv
    df[(df.day .== x[i,1]) .& (df.company .== x[i,2]), Symbol("v", j)] .= join(rand('a':'z', 3), "")
end

这是by保留所有其他变量(假设它们在每组中是恒定的,即使对于相对较大的数据,此代码也应该是有效的):

outdf = by(df, [:company, :day]) do sub
    merge((fullreport = join(sub.report, "\n(Joined)\n"),),
          copy(sub[1, Not([:company, :day, :report])]))
end

我把fullreport变量作为第一个。

这是保留原始数据框中所有行的代码:

outdf = by(df, [:company, :day]) do sub
    insertcols!(select(sub, Not([:company, :day, :report])), 1,
                fullreport = join(sub.report, "\n(Joined)\n"))
end

现在您可以检查是否unique(outdf)生成与 first 生成的数据帧相同的数据帧by

(在上面的代码中,我也删除了:report变量,因为我猜你不希望它出现在结果中 - 对吧?)


推荐阅读