给定一个包含“a”,“b”和“value”列的数据框,我想从每对(“a”,“b”)中采样 N 行。在 python pandas 中,使用以下语法很容易做到这一点:
import pandas as pd
df.groupby(["a", "b"]).sample(n=10)
在 Julia 中,我找到了一种实现类似功能的方法:
using DataFrames, StatsBase
combine(groupby(df, [:a, :b]),
names(df) .=> sample .=> names(df)
)
但是,我不知道如何将其扩展到 n>1。我试过了
combine(groupby(df, [:a, :b]),
names(df) .=> x -> sample(x, n) .=> names(df)
)
但这返回了错误(对于n=3
):
DimensionMismatch("数组不能广播到一个共同的大小;得到一个长度为 3 和 7 的维度")
我发现的一种方法(语法略有不同)是:
combine(groupby(df, [:a, :b]), x -> x[sample(1:nrow(x), n), :])
但我很想知道是否有更好的选择