dataframe - 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
解决方案
以下是您的数据准备代码中的一些小调整:
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
变量,因为我猜你不希望它出现在结果中 - 对吧?)
推荐阅读
- vim - 与我的本地(ubuntu)PC 相比,Vi/Vim 在远程服务器上的行为完全不同
- python - (Python) 完全不熟悉编码,只是尝试一些简单的代码,如果你说好,我想说一件事,如果你说不,我想说另一行
- laravel - required_if 在 Laravel 中有多个字段
- javascript - javascript - 获取多个子数组的最大值/最小值
- c# - 无法通过 POST API 调用提交表单数据
- flutter - 无论使用来自 qr_flutter 包的 QRImage 小部件的 size 属性,QR 图像的大小都不会改变
- python-3.x - 在 python 3.7 中显示 tensorflow 2.4.1 的版本
- c# - Windows 窗体按钮代码
- firebase - Firebase手机身份验证与android 9
- c# - AppContainer 未处理的异常