GroupBy[{elem1,elem2,…},f]
gives an association that groups the elemi into lists associated with distinct keys f[elemi].
GroupBy[{elem1,elem2,…},fkfv]
groups the fv[elemi] according to the fk[elemi].
GroupBy[{elem1,elem2,…},{fs1,fs2,…}]
groups into nested associations using fsi at level i.
GroupBy[{elem1,elem2,…},spec,red]
applies the function red to reduce lists of values that are generated.
GroupBy
GroupBy[{elem1,elem2,…},f]
gives an association that groups the elemi into lists associated with distinct keys f[elemi].
GroupBy[{elem1,elem2,…},fkfv]
groups the fv[elemi] according to the fk[elemi].
GroupBy[{elem1,elem2,…},{fs1,fs2,…}]
groups into nested associations using fsi at level i.
GroupBy[{elem1,elem2,…},spec,red]
applies the function red to reduce lists of values that are generated.
Details
- GroupBy provides a generalization of the map reduce operation.
- GroupBy[list,f] gives an association whose keys are the distinct f[elemi] and whose values are sublists of the list list.
- GroupBy[assoc,f] gives an association whose keys are the distinct f[elemi] and whose values are subassociations of the association assoc.
- GroupBy[spec][expr] is equivalent to GroupBy[expr,spec].
Examples
open all close allBasic Examples (3)
Group pairs of items according to the first element of the pair:
GroupBy[{{a, b}, {a, c}, {b, c}}, First]Group values of an association:
GroupBy[<|a -> 1, b -> 2, c -> 3|>, EvenQ]Group by the first element and compute the mean of the corresponding last elements:
GroupBy[{{a, x}, {b, v}, {a, y}, {a, z}, {b, w}}, First -> Last, Mean]Scope (13)
Group the elements repeatedly by using several functions:
GroupBy[Range[10], {PrimeQ, OddQ}]Group last parts of pairs according to the first part:
GroupBy[{{a, 10}, {b, 20}, {a, 30}}, First -> Last]Group values of an association by using several functions:
GroupBy[<|a -> 1, b -> 2, c -> 4|>, {EvenQ, PrimeQ}]Use a symbolic selector for grouping:
GroupBy[{{a, b}, {a, c}, {b, c}}, f]Pairs for which the selector yields the same value are grouped under the same key:
GroupBy[{-3, -2, -1, 0, 1, 2, 3}, Abs]Use different functions to extract keys and values:
GroupBy[{{a, b}, {a, c}, {b, c}}, f -> g]GroupBy[{{a, b}, {a, c}, {b, c}}, f -> Last]GroupBy[{{a, b}, {a, c}, {b, c}}, First -> Last]Use the operator form of Extract to specify the key or the value:
GroupBy[{{{a}, b}, {{a}, d}}, Extract[{1, 1}]]GroupBy[{{{a}, b}, {{a}, d}}, Extract[2] -> Extract[{1, 1}]]Use a combiner function to combine the values:
GroupBy[{{a, b}, {a, c}, {b, c}}, First -> Last, Total]GroupBy[{{a, 10}, {b, 20}, {a, 30}}, First -> Last, Total]Group associations according to the value of their first element:
GroupBy[{<|1 -> a, 2 -> b|>, <|1 -> a, 2 -> c|>}, First]Group associations according to the value of the given key:
GroupBy[{<|1 -> a, 2 -> b|>, <|2 -> a, 3 -> c|>}, Key[2]]Specify keys and values using Key:
GroupBy[{<|1 -> a, 2 -> c|>, <|1 -> b, 2 -> c|>}, Key[2] -> Key[1]]Missing keys are replaced with Missing:
GroupBy[{<|1 -> a, 2 -> b|>, <|1 -> a, 3 -> c|>}, Key[2]]Group a list of integers by positivity and parity, displaying the groups as column vectors:
GroupBy[Range[-4, 4], {Positive, EvenQ}, MatrixForm]Use the operator form of GroupBy:
GroupBy[First] @ {{a, b}, {a, c}, {b, c}}Group results of applying a function to values of an association:
GroupBy[<|a -> 1, b -> 2, c -> 4|>, EvenQ -> (# + 1&)]Applications (1)
Use GroupBy to group rows in a dataset, introducing new levels of association:
titanic = ExampleData[{"Dataset", "Titanic"}]Group the passengers by class:
titanic[GroupBy["class"]]Group the passengers by sex and then class:
titanic[GroupBy["sex"], GroupBy["class"]]Properties & Relations (3)
GroupBy returns an association, while GatherBy returns a list:
GroupBy[{{a, b}, {a, c}, {b, c}}, First]GatherBy[{{a, b}, {a, c}, {b, c}}, First]Grouping by f is equivalent to grouping by fIdentity:
GroupBy[{a, b, a}, f]GroupBy[{a, b, a}, f -> Identity]Counts[expr] is equivalent to GroupBy[expr,Identity,Length]:
Counts[{a, b, c, a}] == GroupBy[{a, b, c, a}, Identity, Length]CountsBy[expr,f] is equivalent to GroupBy[expr,f,Length]:
CountsBy[{{a, b}, {b, c}, {a, d}}, First] == GroupBy[{{a, b}, {b, c}, {a, d}}, First, Length]See Also
Merge CountsBy GatherBy SplitBy Catenate DeleteDuplicatesBy ParallelCombine
Function Repository: GroupByList KeyGroupBy GroupCases KeyIndex MapReduceOperator
History
Text
Wolfram Research (2014), GroupBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GroupBy.html.
CMS
Wolfram Language. 2014. "GroupBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/GroupBy.html.
APA
Wolfram Language. (2014). GroupBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GroupBy.html
BibTeX
@misc{reference.wolfram_2026_groupby, author="Wolfram Research", title="{GroupBy}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/GroupBy.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_groupby, organization={Wolfram Research}, title={GroupBy}, year={2014}, url={https://reference.wolfram.com/language/ref/GroupBy.html}, note=[Accessed: 12-June-2026]}