Here is a slight modificaiton of Mathematica's example dataset from the documentation:

ds = Dataset[{
   <|"a" -> 1, "b" -> "x", "team" -> {"John"}|>,
   <|"a" -> 2, "b" -> "y", "team" -> {"Mary", "Alice"}|>,
   <|"a" -> 3, "b" -> "z", "team" -> {"Alice"}|>,
   <|"a" -> 4, "b" -> "x", "team" -> {"Lucas", "Charlie"}|>,
   <|"a" -> 5, "b" -> "y", "team" -> {"Charlie", "Bobby", "Ana"}|>,
   <|"a" -> 6, "b" -> "z", "team" -> {}|>}]

Grouping by columns "a" or "b" is easy. But how could I group by column "team" (taking into account, of course, the different entries)?

I think of this as e.g. a dataset containing active projects within a company, and "team" is the list of people involved. What I want to do then is to group the projects by the people involved.

The way I found to do this is as

staff = {"John","Mary", "Alice","Lucas", "Charlie", "Bobby", "Ana"};

Dataset[Association[Table[person -> ds[Select[MemberQ[#team, person] &]], {person, 

But it doesn't look great. And I am not convinced this is the proper way of handling this type of query. This seems like a standard problem and there should be a proper way of dealing with it. But I can't seem to find out.

Any help is appreciated!


1 Answer 1


Here is one way:

ds[Query@GroupBy[First -> Last], Splice@Thread[#team -> #] &]

dataset screeenshot

Unlike the expression given in the question, this does not produce nested datasets. But if nested datasets are desired, then:

ds[Query[GroupBy[First->Last], Dataset], Splice@Thread[#team -> #] &]

nested datasets

  • $\begingroup$ Awesome. This works perfectly. Thank you! $\endgroup$ Mar 7, 2021 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.