Home Explore Blog CI



nushell

4th chunk of `book/working_with_tables.md`
c6f866ac83bbf864ef0f3b1fa25ceede7ad12b011cd58d05000000010000153e
let first = [[a b]; [1 2]]
let second = [[c d]; [3 4]]
$first | merge $second
# => ───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d
# => ───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 3 │ 4
# => ───┴───┴───┴───┴───
```

Let's add a third table:

```nu
let third = [[e f]; [5 6]]
```

We could join all three tables together like this:

```nu
$first | merge $second  | merge $third
# => ───┬───┬───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d │ e │ f
# => ───┼───┼───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6
# => ───┴───┴───┴───┴───┴───┴───
```

Or we could use the [`reduce`](/commands/docs/reduce.md) command to dynamically merge all tables:

```nu
[$first $second $third] | reduce {|elt, acc| $acc | merge $elt }
# => ───┬───┬───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d │ e │ f
# => ───┼───┼───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6
# => ───┴───┴───┴───┴───┴───┴───
```

### Adding a new Column

We can use the [`insert`](/commands/docs/insert.md) command to add a new column to the table. Let's look at an example:

```nu
open rustfmt.toml
# => ─────────┬──────
# =>  edition │ 2018
# => ─────────┴──────
```

Let's add a column called "next_edition" with the value 2021:

```nu
open rustfmt.toml | insert next_edition 2021
# => ──────────────┬──────
# =>  edition      │ 2018
# =>  next_edition │ 2021
# => ──────────────┴──────
```

This visual may be slightly confusing, because it looks like what we've just done is add a row. In this case, remember: rows have numbers, columns have names. If it still is confusing, note that appending one more row will make the table render as expected:

```nu
open rustfmt.toml | insert next_edition 2021 | append {edition: 2021 next_edition: 2024}
# => ───┬─────────┬──────────────
# =>  # │ edition │ next_edition
# => ───┼─────────┼──────────────
# =>  0 │    2018 │         2021
# =>  1 │    2021 │         2024
# => ───┴─────────┴──────────────
```

Notice that if we open the original file, the contents have stayed the same:

```nu
open rustfmt.toml
# => ─────────┬──────
# =>  edition │ 2018
# => ─────────┴──────
```

Changes in Nu are functional changes, meaning that they work on values themselves rather than trying to cause a permanent change. This lets us do many different types of work in our pipeline until we're ready to write out the result with any changes we'd like if we choose to. Here we could write out the result using the [`save`](/commands/docs/save.md) command:

```nu
open rustfmt.toml | insert next_edition 2021 | save rustfmt2.toml
open rustfmt2.toml
# => ──────────────┬──────
# =>  edition      │ 2018
# =>  next_edition │ 2021
# => ──────────────┴──────
```

### Updating a Column

In a similar way to the [`insert`](/commands/docs/insert.md) command, we can also use the [`update`](/commands/docs/update.md) command to change the contents of a column to a new value. To see it in action let's open the same file:

```nu
open rustfmt.toml
# => ─────────┬──────
# =>  edition │ 2018
# => ─────────┴──────
```

And now, let's update the edition to point at the next edition we hope to support:

```nu
open rustfmt.toml | update edition 2021
# => ─────────┬──────
# =>  edition │ 2021
# => ─────────┴──────
```

You can also use the [`upsert`](/commands/docs/upsert.md) command to insert or update depending on whether the column already exists.

### Moving Columns

You can use [`move`](/commands/docs/move.md) to move columns in the table. For example, if we wanted to move the "name" column from [`ls`](/commands/docs/ls.md) after the "size" column, we could do:

```nu
ls | move name --after size
# => ╭────┬──────┬─────────┬───────────────────┬──────────────╮
# => │ #  │ type │  size   │       name        │   modified   │
# => ├────┼──────┼─────────┼───────────────────┼──────────────┤
# => │  0 │ dir  │   256 B │ Applications      │ 3 days ago   │
# => │  1 │ dir  │   256 B │ Data              │ 2 weeks ago  │
# => │  2 │ dir  │   448 B │ Desktop           │ 2 hours ago  │
# => │  3 │ dir  │   192 B │ Disks             │ a week ago   │

Title: Modifying Data Tables: Inserting, Updating, and Moving Columns
Summary
This section details how to modify data tables by inserting a new column with `insert`, which adds a column with a specified name and value. The `update` command changes the values within an existing column, and `upsert` either inserts or updates a column based on its existence. The `move` command rearranges the order of columns within the table.