Home Explore Blog CI



nushell

4th chunk of `book/navigating_structured_data.md`
45e42a1a889fa4ca78c235a36220ca2f76506f1a398f84500000000100001201
# => │ # │    date     │     temps     │ condition │
# => ├───┼─────────────┼───────────────┼───────────┤
# => │ 0 │ 2 years ago │ ╭───┬───────╮ │ sunny     │
# => │   │             │ │ 0 │ 35.24 │ │           │
# => │   │             │ │ 1 │ 35.94 │ │           │
# => │   │             │ │ 2 │ 34.91 │ │           │
# => │   │             │ │ 3 │ 35.24 │ │           │
# => │   │             │ │ 4 │ 36.65 │ │           │
# => │   │             │ ╰───┴───────╯ │           │
# => ╰───┴─────────────┴───────────────┴───────────╯
```

Notice that:

- [`get`](/commands/docs/get.md) returns the same record as the `$data.1` example above
- [`select`](/commands/docs/select.md) returns a new, single-row table, including column names and row indices

::: tip
The row indices of the table resulting from `select` are not the same as that of the original. The new table has its own, 0-based index.

To obtain the original index, you can use the [`enumerate`](/commands/docs/enumerate.md) command. For example:

```nu
$data | enumerate | select 1
```

:::

### Example - `select` with multiple rows and columns

Because `select` results in a new table, it's possible to specify multiple column names, row indices, or even both. This example creates a new table containing the date and condition columns of the first and second rows:

```nu
$data | select date condition 0 1
# => ╭───┬─────────────┬───────────╮
# => │ # │    date     │ condition │
# => ├───┼─────────────┼───────────┤
# => │ 0 │ 2 years ago │ sunny     │
# => │ 1 │ 2 years ago │ sunny     │
# => ╰───┴─────────────┴───────────╯
```

## Key/Column names with spaces

If a key name or column name contains spaces or other characters that prevent it from being accessible as a bare-word string, then the key name may be quoted.

Example:

```nu
let record_example = {
    "key x":12
    "key y":4
  }
$record_example."key x"
# => 12

# or
$record_example | get "key x"
# => 12
```

Quotes are also required when a key name may be confused for a numeric value.

Example:

```nu
let record_example = {
  "1": foo
  "2": baz
  "3": far
}

$record_example."1"
# =>   foo
```

Do not confuse the key name with a row index in this case. Here, the first item is _assigned_ the key name `1` (a string). If converted to a table using the `transpose` command, key `1` (`string`) would be at row-index `0` (an integer).

## Handling Missing Data

### The Optional Operator

By default, cell path access will fail if it can't access the requested row or column. To suppress these errors, you can add a `?` to a cell path member to mark it as optional:

#### Example - The Optional Operator

Using the temp data from above:

```nu
let cp: cell-path = $.temps?.1 # only get the 2nd location from the temps column

# Ooops, we've removed the temps column
$data | reject temps | get $cp
```

By default missing cells will be replaced by `null` when accessed via the optional operator.

### Assigning a `default` for missing or `null` data

The [`default` command](/commands/docs/default.html) can be used to apply a default value to missing or null column result.

```nu
let missing_value = [{a:1 b:2} {b:1}]
$missing_value
# => ╭───┬────┬───╮
# => │ # │ a  │ b │
# => ├───┼────┼───┤
# => │ 0 │  1 │ 2 │
# => │ 1 │ ❎ │ 1 │
# => ╰───┴────┴───╯

let with_default_value = ($missing_value | default 'n/a' a)
$with_default_value
# => ╭───┬─────┬───╮
# => │ # │  a  │ b │
# => ├───┼─────┼───┤
# => │ 0 │   1 │ 2 │
# => │ 1 │ n/a │ 1 │
# => ╰───┴─────┴───╯

$with_default_value.1.a
# => n/a
```

## Other commands for accessing structured data

- [`reject`](/commands/docs/reject.md) is the opposite of `select`, removing the specified rows and columns
- [`range`](/commands/docs/range.md) specifies the rows of a list or table to select using a [`range`](./types_of_data.md#ranges) type

Title: Advanced Data Access Techniques in Nushell: `select`, Quoted Keys, Optional Operator, and Default Values
Summary
This section covers advanced Nushell data access techniques. It demonstrates using `select` with multiple rows and columns, accessing keys with spaces or numeric-like names using quotes, and handling missing data. It introduces the optional operator `?` to suppress errors when accessing potentially missing data, replacing missing cells with `null`. The `default` command is explained for assigning default values to missing or null column results. Finally, it mentions `reject` for removing data and `range` for selecting rows.