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 unnest for dataframe

Decompose struct columns into separate columns for each of their fields. The new columns will be inserted into the dataframe at the location of the struct column.

Signature

> polars unnest {flags} ...rest

Flags

  • --separator, -s {string}: optional separator to use when creating new column names

Parameters

  • ...rest: columns to unnest

Input/output types:

inputoutput
polars_dataframepolars_dataframe
polars_lazyframepolars_lazyframe

Examples

Unnest a dataframe

> [[id person]; [1 {name: "Bob", age: 36}] [2 {name: "Betty", age: 63}]]
                    | polars into-df -s {id: i64, person: {name: str, age: u8}}
                    | polars unnest person
                    | polars get id name age
                    | polars sort-by id
╭───┬────┬───────┬─────╮
│ # │ id │ name  │ age │
├───┼────┼───────┼─────┤
│ 0 │  1 │ Bob   │  36 │
│ 1 │  2 │ Betty │  63 │
╰───┴────┴───────┴─────╯

Unnest a lazy dataframe

> [[id person]; [1 {name: "Bob", age: 36}] [2 {name: "Betty", age: 63}]]
                    | polars into-df -s {id: i64, person: {name: str, age: u8}}
                    | polars into-lazy
                    | polars unnest person
                    | polars select (polars col id) (polars col name) (polars col age)
                    | polars collect
                    | polars sort-by id
╭───┬────┬───────┬─────╮
│ # │ id │ name  │ age │
├───┼────┼───────┼─────┤
│ 0 │  1 │ Bob   │  36 │
│ 1 │  2 │ Betty │  63 │
╰───┴────┴───────┴─────╯

Unnest with a custom separator

> [[id person]; [1 {name: "Bob", age: 36}] [2 {name: "Betty", age: 63}]]
                    | polars into-df -s {id: i64, person: {name: str, age: u8}}
                    | polars unnest person -s "_"
                    | polars get id person_name person_age
                    | polars sort-by id
╭───┬────┬─────────────┬────────────╮
│ # │ id │ person_name │ person_age │
├───┼────┼─────────────┼────────────┤
│ 0 │  1 │ Bob         │         36 │
│ 1 │  2 │ Betty       │         63 │
╰───┴────┴─────────────┴────────────╯