---
title: "GroupBy"
language: "en"
type: "Symbol"
summary: "GroupBy[{elem1, elem2, ...}, f] gives an association that groups the elemi into lists associated with distinct keys f[elemi]. GroupBy[{elem1, elem2, ...}, fk -> fv] 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."
keywords: 
- map reduce
- hadoop
canonical_url: "https://reference.wolfram.com/language/ref/GroupBy.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Associations"
    link: "https://reference.wolfram.com/language/guide/Associations.en.md"
  - 
    title: "Database-Like Operations on Datasets"
    link: "https://reference.wolfram.com/language/guide/DatabaseLikeOperationsOnDatasets.en.md"
  - 
    title: "Computation with Structured Datasets"
    link: "https://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.en.md"
  - 
    title: "List Manipulation"
    link: "https://reference.wolfram.com/language/guide/ListManipulation.en.md"
  - 
    title: "Functional Programming"
    link: "https://reference.wolfram.com/language/guide/FunctionalProgramming.en.md"
related_functions: 
  - 
    title: "Merge"
    link: "https://reference.wolfram.com/language/ref/Merge.en.md"
  - 
    title: "CountsBy"
    link: "https://reference.wolfram.com/language/ref/CountsBy.en.md"
  - 
    title: "GatherBy"
    link: "https://reference.wolfram.com/language/ref/GatherBy.en.md"
  - 
    title: "SplitBy"
    link: "https://reference.wolfram.com/language/ref/SplitBy.en.md"
  - 
    title: "Catenate"
    link: "https://reference.wolfram.com/language/ref/Catenate.en.md"
  - 
    title: "DeleteDuplicatesBy"
    link: "https://reference.wolfram.com/language/ref/DeleteDuplicatesBy.en.md"
  - 
    title: "ParallelCombine"
    link: "https://reference.wolfram.com/language/ref/ParallelCombine.en.md"
---
# GroupBy

GroupBy[{elem1, elem2, …}, f] gives an association that groups the elemi into lists associated with distinct keys f[elemi].

GroupBy[{elem1, elem2, …}, fk -> fv] 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 (18)

### Basic Examples (3)

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

```wl
In[1]:= GroupBy[{{a, b}, {a, c}, {b, c}}, First]

Out[1]= <|a -> {{a, b}, {a, c}}, b -> {{b, c}}|>
```

---

Group values of an association:

```wl
In[1]:= GroupBy[<|a -> 1, b -> 2, c -> 3|>, EvenQ]

Out[1]= <|False -> <|a -> 1, c -> 3|>, True -> <|b -> 2|>|>
```

---

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

```wl
In[1]:= GroupBy[{{a, x}, {b, v}, {a, y}, {a, z}, {b, w}}, First -> Last, Mean]

Out[1]= <|a -> (1/3) (x + y + z), b -> (v + w/2)|>
```

### Scope (12)

Group the elements repeatedly by using several functions:

```wl
In[1]:= GroupBy[Range[10], {PrimeQ, OddQ}]

Out[1]= <|True -> <|False -> {2}, True -> {3, 5, 7}|>, False -> <|True -> {1, 9}, False -> {4, 6, 8, 10}|>|>
```

---

Group last parts of pairs according to the first part:

```wl
In[1]:= GroupBy[{{a, 10}, {b, 20}, {a, 30}}, First -> Last]

Out[1]= <|a -> {10, 30}, b -> {20}|>
```

---

Group values of an association by using several functions:

```wl
In[1]:= GroupBy[<|a -> 1, b -> 2, c -> 4|>, {EvenQ, PrimeQ}]

Out[1]= <|True -> <|True -> <|b -> 2|>, False -> <|c -> 4|>|>, False -> <|False -> <|a -> 1|>|>|>
```

---

Use a symbolic selector for grouping:

```wl
In[1]:= GroupBy[{{a, b}, {a, c}, {b, c}}, f]

Out[1]= <|f[{a, b}] -> {{a, b}}, f[{a, c}] -> {{a, c}}, f[{b, c}] -> {{b, c}}|>
```

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

```wl
In[2]:= GroupBy[{-3, -2, -1, 0, 1, 2, 3}, Abs]

Out[2]= <|3 -> {-3, 3}, 2 -> {-2, 2}, 1 -> {-1, 1}, 0 -> {0}|>
```

---

Use different functions to extract keys and values:

```wl
In[1]:= GroupBy[{{a, b}, {a, c}, {b, c}}, f -> g]

Out[1]= <|f[{a, b}] -> {g[{a, b}]}, f[{a, c}] -> {g[{a, c}]}, f[{b, c}] -> {g[{b, c}]}|>

In[2]:= GroupBy[{{a, b}, {a, c}, {b, c}}, f -> Last]

Out[2]= <|f[{a, b}] -> {b}, f[{a, c}] -> {c}, f[{b, c}] -> {c}|>

In[3]:= GroupBy[{{a, b}, {a, c}, {b, c}}, First -> Last]

Out[3]= <|a -> {b, c}, b -> {c}|>
```

---

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

```wl
In[1]:= GroupBy[{{{a}, b}, {{a}, d}}, Extract[{1, 1}]]

Out[1]= <|a -> {{{a}, b}, {{a}, d}}|>

In[2]:= GroupBy[{{{a}, b}, {{a}, d}}, Extract[2] -> Extract[{1, 1}]]

Out[2]= <|b -> {a}, d -> {a}|>
```

---

Use a combiner function to combine the values:

```wl
In[1]:= GroupBy[{{a, b}, {a, c}, {b, c}}, First -> Last, Total]

Out[1]= <|a -> b + c, b -> c|>

In[2]:= GroupBy[{{a, 10}, {b, 20}, {a, 30}}, First -> Last, Total]

Out[2]= <|a -> 40, b -> 20|>
```

---

Group associations according to the value of their first element:

```wl
In[1]:= GroupBy[{<|1 -> a, 2 -> b|>, <|1 -> a, 2 -> c|>}, First]

Out[1]= <|a -> {<|1 -> a, 2 -> b|>, <|1 -> a, 2 -> c|>}|>
```

Group associations according to the value of the given key:

```wl
In[2]:= GroupBy[{<|1 -> a, 2 -> b|>, <|2 -> a, 3 -> c|>}, Key[2]]

Out[2]= <|b -> {<|1 -> a, 2 -> b|>}, a -> {<|2 -> a, 3 -> c|>}|>
```

---

Specify keys and values using ``Key`` :

```wl
In[1]:= GroupBy[{<|1 -> a, 2 -> c|>, <|1 -> b, 2 -> c|>}, Key[2] -> Key[1]]

Out[1]= <|c -> {a, b}|>
```

---

Missing keys are replaced with ``Missing`` :

```wl
In[1]:= GroupBy[{<|1 -> a, 2 -> b|>, <|1 -> a, 3 -> c|>}, Key[2]]

Out[1]= <|b -> {<|1 -> a, 2 -> b|>}, Missing["KeyAbsent", 2] -> {<|1 -> a, 3 -> c|>}|>
```

---

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

```wl
In[1]:= GroupBy[Range[-4, 4], {Positive, EvenQ}, MatrixForm]

Out[1]=
<|True -> <|False -> (⁠|   |
| - |
| 1 |
| 3 |⁠), True -> (⁠|   |
| - |
| 2 |
| 4 |⁠)|>, False -> <|True -> (⁠|    |
| -- |
| -4 |
| -2 |
| 0  |⁠), False -> (⁠|    |
| -- |
| -3 |
| -1 |⁠)|>|>
```

---

Use the operator form of ``GroupBy`` :

```wl
In[1]:= GroupBy[First] @ {{a, b}, {a, c}, {b, c}}

Out[1]= <|a -> {{a, b}, {a, c}}, b -> {{b, c}}|>
```

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

```wl
In[2]:= GroupBy[<|a -> 1, b -> 2, c -> 4|>, EvenQ -> (# + 1&)]

Out[2]= <|False -> <|a -> 2|>, True -> <|b -> 3, c -> 5|>|>
```

### Applications (1)

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

```wl
In[1]:= titanic = ExampleData[{"Dataset", "Titanic"}]

Out[1]= Dataset[<>]
```

Group the passengers by class:

```wl
In[2]:= titanic[GroupBy["class"]]

Out[2]= Dataset[<>]
```

Group the passengers by sex and then class:

```wl
In[3]:= titanic[GroupBy["sex"], GroupBy["class"]]

Out[3]= Dataset[<>]
```

### Properties & Relations (2)

``GroupBy`` returns an association, while ``GatherBy`` returns a list:

```wl
In[1]:= GroupBy[{{a, b}, {a, c}, {b, c}}, First]

Out[1]= <|a -> {{a, b}, {a, c}}, b -> {{b, c}}|>

In[2]:= GatherBy[{{a, b}, {a, c}, {b, c}}, First]

Out[2]= {{{a, b}, {a, c}}, {{b, c}}}
```

---

Grouping by ``f`` is equivalent to grouping by ``f -> Identity`` :

```wl
In[1]:= GroupBy[{a, b, a}, f]

Out[1]= <|f[a] -> {a, a}, f[b] -> {b}|>

In[2]:= GroupBy[{a, b, a}, f -> Identity]

Out[2]= <|f[a] -> {a, a}, f[b] -> {b}|>
```

## See Also

* [`Merge`](https://reference.wolfram.com/language/ref/Merge.en.md)
* [`CountsBy`](https://reference.wolfram.com/language/ref/CountsBy.en.md)
* [`GatherBy`](https://reference.wolfram.com/language/ref/GatherBy.en.md)
* [`SplitBy`](https://reference.wolfram.com/language/ref/SplitBy.en.md)
* [`Catenate`](https://reference.wolfram.com/language/ref/Catenate.en.md)
* [`DeleteDuplicatesBy`](https://reference.wolfram.com/language/ref/DeleteDuplicatesBy.en.md)
* [`ParallelCombine`](https://reference.wolfram.com/language/ref/ParallelCombine.en.md)

## Related Guides

* [`Associations`](https://reference.wolfram.com/language/guide/Associations.en.md)
* [Database-Like Operations on Datasets](https://reference.wolfram.com/language/guide/DatabaseLikeOperationsOnDatasets.en.md)
* [Computation with Structured Datasets](https://reference.wolfram.com/language/guide/ComputationWithStructuredDatasets.en.md)
* [List Manipulation](https://reference.wolfram.com/language/guide/ListManipulation.en.md)
* [Functional Programming](https://reference.wolfram.com/language/guide/FunctionalProgramming.en.md)

## History

* [Introduced in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md)