dataframe - Julia:为 DataFrame 的 y 列中的每个唯一值创建 x 列的汇总值

标签 dataframe julia

我想对 DataFramex 列应用一些函数,例如均值和方差,以获取 y 列中的每个唯一值。我可以想象构建一个循环,手动对 DataFrame 进行子集化以完成我的目的,但我试图不为可能是常见功能的东西重新发明轮子。

using DataFrames
mydf = DataFrame(y = [randstring(1) for i in 1:1000], x = rand(1000))
# I could imagine a function that looks like:
apply(function = mean, across = mydf[:x], by = mydf[:y])

最佳答案

你说得对,这很常见。看看split-apply-combine文档中的章节。这里有几种方法:您可以使用更通用的 by 函数来准确指定要操作的列,也可以使用方便的 aggregate 函数来使用所有其他列并自动合理地命名它们:

julia> aggregate(mydf, :y, mean)
62×2 DataFrames.DataFrame
│ Row │ y   │ x_mean   │
├─────┼─────┼──────────┤
│ 1   │ "0" │ 0.454196 │
│ 2   │ "1" │ 0.541434 │
│ 3   │ "2" │ 0.36734  │
⋮

关于dataframe - Julia:为 DataFrame 的 y 列中的每个唯一值创建 x 列的汇总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948804/

相关文章:

r - 根据每组的其他列匹配列的值

julia - Julia 中的扫描等价物

python - 如何在 DataFrame 中获取唯一的值对

python - 如何检查 pandas 数据框中的值范围?

julia - Julia using 对丢失的包有何表现?

Julia 密谋 : how to add both a single point and a list of points to a scatter plot

dictionary - 字典 vs NamedTuples

csv - 读取 csv 的第二列

java - Spark DataFrame 类的 union() 方法在哪里?

python - 按特定类对一系列数据帧进行计数