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
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

polars unpivot for dataframe

Unpivot a DataFrame from wide to long format.

This command requires a plugin

The polars unpivot command resides in the polars plugin. To use this command, you must install and register nu_plugin_polars. See the Plugins chapter in the book for more information.

Signature

> polars unpivot {flags}

Flags

  • --index, -i {list<any>}: column names for unpivoting
  • --on, -o {list<any>}: column names used as value columns
  • --variable-name, -r {string}: optional name for variable column
  • --value-name, -l {string}: optional name for value column

Input/output types:

inputoutput
dataframedataframe

Examples

unpivot on an eager dataframe

> [[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | polars into-df | polars unpivot -i [b c] -o [a d]
╭───┬───┬───┬──────────┬───────╮
│ # │ b │ c │ variable │ value │
├───┼───┼───┼──────────┼───────┤
│ 0 │ 1 │ 4 │ a        │ x     │
│ 1 │ 2 │ 5 │ a        │ y     │
│ 2 │ 3 │ 6 │ a        │ z     │
│ 3 │ 1 │ 4 │ d        │ a     │
│ 4 │ 2 │ 5 │ d        │ b     │
│ 5 │ 3 │ 6 │ d        │ c     │
╰───┴───┴───┴──────────┴───────╯

unpivot on a lazy dataframe

> [[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | polars into-lazy | polars unpivot -i [b c] -o [a d] | polars collect
╭───┬───┬───┬──────────┬───────╮
│ # │ b │ c │ variable │ value │
├───┼───┼───┼──────────┼───────┤
│ 0 │ 1 │ 4 │ a        │ x     │
│ 1 │ 2 │ 5 │ a        │ y     │
│ 2 │ 3 │ 6 │ a        │ z     │
│ 3 │ 1 │ 4 │ d        │ a     │
│ 4 │ 2 │ 5 │ d        │ b     │
│ 5 │ 3 │ 6 │ d        │ c     │
╰───┴───┴───┴──────────┴───────╯