# Select dataset columns after GroupBy

Very basic question. Probably showing my ignorance, but if I have a dataset like the example in the docs

dataset = Dataset[{
<|"a" -> 1, "b" -> "x", "c" -> {1}|>,
<|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
<|"a" -> 3, "b" -> "z", "c" -> {3}|>,
<|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
<|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
<|"a" -> 6, "b" -> "z", "c" -> {}|>}]


and I want to group by column "b" I type

dataset[GroupBy["b"]]


I get the beautiful result

But if I just want columns "a" and "c", how do I get them? Naively I type

dataset[GroupBy["b"], {"a", "c"}]


but that crashes and burns

What's going on? How do I select what columns I want after GroupBy?

• Note you get a nested dataset after you GroupBy["b"]
– yode
Jul 18 '17 at 15:53
• So GroupBy doesn't "descend"? Jul 18 '17 at 16:44
• DatasetAscendingQ and DatasetDescendingQ can tell you it is or not.
– yode
Jul 18 '17 at 16:53

dataset[GroupBy[Key["b"] -> KeyDrop["b"]]]


or

dataset[GroupBy[Key["b"] -> KeyTake[{"a", "c"}]]]


or

dataset[GroupBy["b"], KeyTake[{"a", "c"}]]


or

dataset[GroupBy["b"], All, {"a", "c"}]

• Thanks for the answer, but it's only half what I'm trying to understand. What specifically is wrong with dataset[GroupBy["b"], {"a", "c"}]? Why is the "All" needed? How would I have known that? What did the error message try to tell me? Jul 18 '17 at 15:54
• @ChrisNadovich I try to stay away from explaining to much when it comes to datasets. I failed to grasp details and there are people around who didn't fail while I'm just using them intuitively :) You can hold on with an accept to not discourage those folks from answering.
– Kuba
Jul 19 '17 at 8:29
• Intuition never seems to work for me with these Mathematica Dataset things. I do just fine with SQL intuition, when using SQL, but it leads me to madness here. Anyway, I think I found the answer I was looking for. Group by is considered "descending" (you can look it up!) but it, in fact, does not descend. It's a kind of a non-operator. So that's why the "All" is needed: because All would have been needed to descend past the rows without the GroupBy, so it's still needed. Jul 21 '17 at 17:02

This answer (19542) is incorrect in stating that GroupBy is not a descending operator. Evaluate the following:

ClearAll[f];
dataset[GroupBy["b"] /* f]

f[<|"x" -> {<|"a" -> 1, "b" -> "x", "c" -> {1}|>,
<|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>},
"y" -> {<|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
<|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>},
"z" -> {<|"a" -> 3, "b" -> "z", "c" -> {3}|>,
<|"a" -> 6, "b" -> "z", "c" -> {}|>}
|>]


When you GroupBy you add another level to the Association hierarchy. The first level (where f is above) contains the keys of the grouping.

dataset[GroupBy["b"], f]


The second level (where f is above) contains all the records that have been grouped. This is a list of Associations. You must indicate which of these Associations in the list you want to access. In this case all are wanted so All is specified.

dataset[GroupBy["b"], All, f]


f is now at the level where the Associations can be accessed. Here you can enter the keys of the items you wish to return.

dataset[GroupBy["b"], All, {"a", "b"}]


As can be seen from the steps above, GroupBy does act while descending. The issue the OP has (hopefully now: had) is not understanding the affect of effecting GroupBy.

Hope this helps.

Practical answers are given by @Kuba, but the real answer I was looking for is this.

Even though GroupBy is considered "descending" (you can look it up!), it, in fact, does not descend. It's a kind of a non-operator. So that's why selecting the columns doesn't work in my question.

First I need to start at the top level and move past the rows. So the All is needed: because All would have been needed to descend past the rows without the GroupBy, so it's still needed.