# DataFrame how to \`groupby\` then index with unspecified keys (merge them)

**URL:** <https://discourse.julialang.org/t/dataframe-how-to-groupby-then-index-with-unspecified-keys-merge-them/90248>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [November 14, 2022, 5:06pm UTC](https://discourse.julialang.org/t/dataframe-how-to-groupby-then-index-with-unspecified-keys-merge-them/90248 "2022-11-14T17:06:22Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 15, 2022, 3:59am UTC](https://discourse.julialang.org/t/dataframe-how-to-groupby-then-index-with-unspecified-keys-merge-them/90248/13 "2022-11-15T03:59:22Z")

</div>

For general Julia collections and for many table types, see `group` + `addmargins` from [FlexiGroups.jl](https://discourse.julialang.org/t/ann-flexigroups-jl-composable-and-general-dataset-group-bys/88998):

```julia
using FlexiGroups

tbl = ...
gm = group(x -> (;x.gender, x.nationality, x.hair_color), tbl) |> addmargins
# these should work - use `total` to select
# the group containing all values of the corresponding parameter:
gm[(; gender = :X, natiaonlity = :US, hair_color=:blue)]
gm[(; gender = :X, natiaonlity = :US, hair_color=total)]
gm[(; gender = total, natiaonlity = :US, hair_color=total)]

```

For dense multidimensional grouping (as your case seems to be), keyed arrays can be convenient instead of dictionaries (the default). See the docs, `FlexiGroups` support this as well.

I don’t think `DataFrames` and `FlexiGroups` work together though, so the above can only indirectly be applied in your specific case as you start from a `DataFrame`.

---

_[View the full topic](https://discourse.julialang.org/t/dataframe-how-to-groupby-then-index-with-unspecified-keys-merge-them/90248)._
