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.

GroupBy[spec]

represents an operator form of GroupBy that can be applied to an expression.

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 allclose all

Basic Examples  (3)

Group pairs of items according to the first element of the pair:

Group values of an association:

Group by the first element and compute the mean of the corresponding last elements:

Scope  (12)

Group the elements repeatedly by using several functions:

Group last parts of pairs according to the first part:

Group values of an association by using several functions:

Use a symbolic selector for grouping:

Pairs for which the selector yields the same value are grouped under the same key:

Use different functions to extract keys and values:

Use the operator form of Extract to specify the key or the value:

Use a combiner function to combine the values:

Group associations according to the value of their first element:

Group associations according to the value of the given key:

Specify keys and values using Key:

Missing keys are replaced with Missing:

Group a list of integers by positivity and parity, displaying the groups as column vectors:

Use the operator form of GroupBy:

Group results of applying a function to values of an association:

Applications  (1)

Use GroupBy to group rows in a dataset, introducing new levels of association:

Group the passengers by class:

Group the passengers by sex and then class:

Properties & Relations  (2)

GroupBy returns an association, while GatherBy returns a list:

Grouping by f is equivalent to grouping by fIdentity:

Wolfram Research (2014), GroupBy, Wolfram Language function, https://reference.wolfram.com/language/ref/GroupBy.html.

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_2024_groupby, author="Wolfram Research", title="{GroupBy}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/GroupBy.html}", note=[Accessed: 27-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_groupby, organization={Wolfram Research}, title={GroupBy}, year={2014}, url={https://reference.wolfram.com/language/ref/GroupBy.html}, note=[Accessed: 27-November-2024 ]}