Home Explore Blog CI



nushell

1st chunk of `commands/docs/group-by.md`
dd1cd63d99ad35798ce5981d326574280adc7751b4fc4b2c000000010000102f
---
title: group-by
categories: |
  filters
version: 0.104.0
filters: |
  Splits a list or table into groups, and returns a record containing those groups.
usage: |
  Splits a list or table into groups, and returns a record containing those groups.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

# `group-by` for [filters](/commands/categories/filters.md)

<div class='command-title'>Splits a list or table into groups, and returns a record containing those groups.</div>

## Signature

```> group-by {flags} ...rest```

## Flags

 -  `--to-table`: Return a table with "groups" and "items" columns

## Parameters

 -  `...rest`: The path to the column to group on.


## Input/output types:

| input     | output |
| --------- | ------ |
| list\<any\> | any    |
## Examples

Group items by the "type" column's values
```nu
> ls | group-by type

```

Group items by the "foo" column's values, ignoring records without a "foo" column
```nu
> open cool.json | group-by foo?

```

Group using a block which is evaluated against each input value
```nu
> [foo.txt bar.csv baz.txt] | group-by { path parse | get extension }
╭─────┬─────────────────╮
│     │ ╭───┬─────────╮ │
│ txt │ │ 0 │ foo.txt │ │
│     │ │ 1 │ baz.txt │ │
│     │ ╰───┴─────────╯ │
│     │ ╭───┬─────────╮ │
│ csv │ │ 0 │ bar.csv │ │
│     │ ╰───┴─────────╯ │
╰─────┴─────────────────╯
```

You can also group by raw values by leaving out the argument
```nu
> ['1' '3' '1' '3' '2' '1' '1'] | group-by
╭───┬───────────╮
│   │ ╭───┬───╮ │
│ 1 │ │ 0 │ 1 │ │
│   │ │ 1 │ 1 │ │
│   │ │ 2 │ 1 │ │
│   │ │ 3 │ 1 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 3 │ │ 0 │ 3 │ │
│   │ │ 1 │ 3 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 2 │ │ 0 │ 2 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯
```

You can also output a table instead of a record
```nu
> ['1' '3' '1' '3' '2' '1' '1'] | group-by --to-table
╭───┬───────┬───────────╮
│ # │ group │   items   │
├───┼───────┼───────────┤
│ 0 │ 1     │ ╭───┬───╮ │
│   │       │ │ 0 │ 1 │ │
│   │       │ │ 1 │ 1 │ │
│   │       │ │ 2 │ 1 │ │
│   │       │ │ 3 │ 1 │ │
│   │       │ ╰───┴───╯ │
│ 1 │ 3     │ ╭───┬───╮ │
│   │       │ │ 0 │ 3 │ │
│   │       │ │ 1 │ 3 │ │
│   │       │ ╰───┴───╯ │
│ 2 │ 2     │ ╭───┬───╮ │
│   │       │ │ 0 │ 2 │ │
│   │       │ ╰───┴───╯ │
╰───┴───────┴───────────╯

```

Group bools, whether they are strings or actual bools
```nu
> [true "true" false "false"] | group-by
╭───────┬───────────────╮
│       │ ╭───┬──────╮  │
│ true  │ │ 0 │ true │  │
│       │ │ 1 │ true │  │
│       │ ╰───┴──────╯  │
│       │ ╭───┬───────╮ │
│ false │ │ 0 │ false │ │
│       │ │ 1 │ false │ │
│       │ ╰───┴───────╯ │
╰───────┴───────────────╯
```

Group items by multiple columns' values
```nu
> [
        [name, lang, year];
        [andres, rb, "2019"],

Title: group-by command documentation
Summary
This document describes the `group-by` command, which splits a list or table into groups and returns a record (or table, with the `--to-table` flag) containing those groups. It details the command's signature, flags, parameters, input/output types, and provides several usage examples, including grouping by column values, using a block for grouping, and grouping by raw values.