Skip to content

Instantly share code, notes, and snippets.

@paul-d-ray
Created October 11, 2025 22:36
Show Gist options
  • Select an option

  • Save paul-d-ray/af3119635422346cad6d348c7032990a to your computer and use it in GitHub Desktop.

Select an option

Save paul-d-ray/af3119635422346cad6d348c7032990a to your computer and use it in GitHub Desktop.
Visidata Nushell Join two data sets

VisiData example done in Nushell

This will use the faa-wildlife-strikes.csv file from the VisiData demo. Calculate each species’ state-level percentages

Nushell Code to Join two data sets

let data = open faa-wildlife-strikes.csv
  | where SPECIES !~ 'Unknown' and STATE != ''

let state = $data
  | group-by --to-table STATE SPECIES
  | each { |g|
     {
        STATE: $g.STATE,
        SPECIES: $g.SPECIES,
        count: ($g.items | length)
      }
    }

 let state_totals = $data
  | group-by --to-table STATE
  | each { |g|
    {
        STATE: $g.STATE,
        state_total: ($g.items | length)
      }
    }

 $state
  | join $state_totals STATE
  | upsert  pct_of_state { |row| ($row.count / $row.state_total * 100) | math round -p 2 }
  | select STATE SPECIES count state_total pct_of_state
  | sort-by pct_of_state -r
  | where count >= 50 

Nushell Code to only show the top two birds per state

# top two birds per state
let data = open faa-wildlife-strikes.csv
   | where SPECIES !~ 'Unknown' and STATE != ''
 let state = $data
   | group-by --to-table STATE SPECIES
   | each { |g|
      {
         STATE: $g.STATE,
         SPECIES: $g.SPECIES,
         count: ($g.items | length)
       }
     }
  let state_totals = $data
   | group-by --to-table STATE
   | each { |g|
     {
         STATE: $g.STATE,
         state_total: ($g.items | length)
       }
     }
  $state
   | join $state_totals STATE
   | upsert pct_of_state { |row| ($row.count / $row.state_total * 100) | math round -p 2 }
   | where count >= 50
   | group-by --to-table STATE
   | each { |g| $g.items | sort-by pct_of_state -r | first 2 }
   | flatten
   | sort-by  pct_of_state -r
   | group-by --to-table STATE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment