Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
  • Cookbook

    • Cookbook
    • Setup
    • Help
    • System
    • Parsing
    • Foreign Shell Scripts
    • Pattern Matching
    • External Completers
    • Module Scenarios
    • Files
    • Git
    • Parsing Git Log
    • Acting on keypresses using `input listen`
    • HTTP
    • Direnv
    • ssh-agent
    • Advanced table workflows
    • Polars vs Pandas vs Nushell
    • jq vs Nushell

Advanced table workflows

Merging tables of different size

Examples shown in Working with tables work fine when our tables have equal amount of rows but what if we want to merge tables of different sizes?

let first_table = [[a b]; [1 2] [3 4]]
let second_table = [[c d]; [5 6]]
$first_table | merge $second_table
# => ───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d
# => ───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 5 │ 6
# => ───┼───┼───┼───┼───
# =>  1 │ 3 │ 4 │ ❎│ ❎
# => ───┴───┴───┴───┴───

Columns c and d in the second row are empty because our second table only contained a single row; Nushell has nothing to fill the remaining rows with. But what if we wanted the smaller table to 'wrap around' and keep filling the rows? For that we can use the chunks command to split the larger table into subtables, merge each of them with the smaller table and then combine the merged tables together using flatten command

For example:

let first_table = [[a b]; [1 2] [3 4]]
let second_table = [[c d]; [5 6]]

$first_table
| chunks ($second_table | length)
| each { merge $second_table }
| flatten
# => ───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d
# => ───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 5 │ 6
# => ───┼───┼───┼───┼───
# =>  1 │ 3 │ 4 │ 5 │ 6
# => ───┴───┴───┴───┴───

Can we do that with more than two tables? Sure we can! Let's add a third table:

let third_table = [[e f]; [7 8]]

We can merge all three tables like this:

$first_table
| chunks ($second_table | length)
| each { merge $second_table }
| flatten
| chunks ($third_table | length)
| each { merge $third_table }
| flatten
# => ───┬───┬───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d │ e │ f
# => ───┼───┼───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 5 │ 6 │ 7 │ 8
# => ───┼───┼───┼───┼───┼───┼───
# =>  1 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8
# => ───┴───┴───┴───┴───┴───┴───

Or as mentioned in the Cookbook we can use the reduce command to merge tables together recursively:

[$first_table $second_table $third_table]
| reduce { |elt, acc|
    $acc
    | chunks ($elt | length)
    | each { merge $elt }
    | flatten
  }
# => ───┬───┬───┬───┬───┬───┬───
# =>  # │ a │ b │ c │ d │ e │ f
# => ───┼───┼───┼───┼───┼───┼───
# =>  0 │ 1 │ 2 │ 5 │ 6 │ 7 │ 8
# => ───┼───┼───┼───┼───┼───┼───
# =>  1 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8
# => ───┴───┴───┴───┴───┴───┴───
Edit this page on GitHub
Contributors: Yethal, Hofer-Julian, Justin Ma, lavafroth, arnau, jaudiger, Ian Manske, NotTheDr01ds, Bruce Weirdan, Jan Klass
Prev
ssh-agent
Next
Polars vs Pandas vs Nushell