Home > Software design >  How to get name of dataframe group?
How to get name of dataframe group?

Time:09-04

When iterating over a GroupedDataFrame, how can one get the name/key of the group?

E.g. access what a equals for each group:

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

CodePudding user response:

Just like iterating over a pairs(dict) to get both keys and values of a dictionary, you can iterate over pairs(gd) to get GroupKeys and values:

julia> for (k, g) in pairs(gd)
         println(k)
       end
GroupKey: (a = 1,)
GroupKey: (a = 2,)
GroupKey: (a = 3,)
GroupKey: (a = 4,)

This behaves like a NamedTuple, and has all the keys as names - in case you have multiple columns you have grouped by. In your case, you can access the value of a for a given group with k.a.

  • Related