当迭代 a 时GroupedDataFrame
,如何获取组的名称/键?
例如,访问a
每个组的相等值:
df = DataFrame(a=repeat([1, 2, 3, 4], outer=[2]),
b=repeat([2, 1], outer=[4]),
c=1:8);
gd = groupby(df, :a)
for g in gd
#... do something with the dataframe and the key of the dataframe
end
Run Code Online (Sandbox Code Playgroud)
就像迭代 apairs(dict)
来获取字典的键和值一样,您可以迭代pairs(gd)
来获取GroupKey
s 和值:
julia> for (k, g) in pairs(gd)
println(k)
end
GroupKey: (a = 1,)
GroupKey: (a = 2,)
GroupKey: (a = 3,)
GroupKey: (a = 4,)
Run Code Online (Sandbox Code Playgroud)
其行为类似于NamedTuple
, 并具有所有键names
- 以防您有多个分组依据的列。a
在您的情况下,您可以使用 访问给定组的值k.a
。