polars into-df
for dataframe
Converts a list, table or record into a dataframe.
This command requires a plugin
The polars into-df
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 into-df {flags}
Flags
--schema, -s {any}
: Polars Schema in format [{name: str}].--as-columns, -c
: When input shape is record of lists, treat each list as column values.
Input/output types:
input | output |
---|---|
any | dataframe |
Examples
Takes a dictionary and creates a dataframe
> [[a b];[1 2] [3 4]] | polars into-df
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 2 │
│ 1 │ 3 │ 4 │
╰───┴───┴───╯
Takes a record of lists and creates a dataframe
> {a: [1 3], b: [2 4]} | polars into-df --as-columns
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 2 │
│ 1 │ 3 │ 4 │
╰───┴───┴───╯
Takes a list of tables and creates a dataframe
> [[1 2 a] [3 4 b] [5 6 c]] | polars into-df
╭───┬───┬───┬───╮
│ # │ 0 │ 1 │ 2 │
├───┼───┼───┼───┤
│ 0 │ 1 │ 2 │ a │
│ 1 │ 3 │ 4 │ b │
│ 2 │ 5 │ 6 │ c │
╰───┴───┴───┴───╯
Takes a list and creates a dataframe
> [a b c] | polars into-df
╭───┬───╮
│ # │ 0 │
├───┼───┤
│ 0 │ a │
│ 1 │ b │
│ 2 │ c │
╰───┴───╯
Takes a list of booleans and creates a dataframe
> [true true false] | polars into-df
╭───┬───────╮
│ # │ 0 │
├───┼───────┤
│ 0 │ true │
│ 1 │ true │
│ 2 │ false │
╰───┴───────╯
Convert to a dataframe and provide a schema
> [[a b c e]; [1 {d: [1 2 3]} [10 11 12] 1.618]]| polars into-df -s {a: u8, b: {d: list<u64>}, c: list<u8>, e: 'decimal<4,3>'}
╭───┬───┬───────────────────┬────────────┬──────╮
│ # │ a │ b │ c │ e │
├───┼───┼───────────────────┼────────────┼──────┤
│ 0 │ 1 │ ╭───┬───────────╮ │ ╭───┬────╮ │ 1.62 │
│ │ │ │ │ ╭───┬───╮ │ │ │ 0 │ 10 │ │ │
│ │ │ │ d │ │ 0 │ 1 │ │ │ │ 1 │ 11 │ │ │
│ │ │ │ │ │ 1 │ 2 │ │ │ │ 2 │ 12 │ │ │
│ │ │ │ │ │ 2 │ 3 │ │ │ ╰───┴────╯ │ │
│ │ │ │ │ ╰───┴───╯ │ │ │ │
│ │ │ ╰───┴───────────╯ │ │ │
╰───┴───┴───────────────────┴────────────┴──────╯
Convert to a dataframe and provide a schema that adds a new column
> [[a b]; [1 "foo"] [2 "bar"]] | polars into-df -s {a: u8, b:str, c:i64} | polars fill-null 3
╭───┬───┬─────┬───╮
│ # │ a │ b │ c │
├───┼───┼─────┼───┤
│ 0 │ 1 │ foo │ 3 │
│ 1 │ 2 │ bar │ 3 │
╰───┴───┴─────┴───╯
If a provided schema specifies a subset of columns, only those columns are selected
> [[a b]; [1 "foo"] [2 "bar"]] | polars into-df -s {a: str}
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ 1 │
│ 1 │ 2 │
╰───┴───╯
Use a predefined schama
> let schema = {a: str, b: str}; [[a b]; [1 "foo"] [2 "bar"]] | polars into-df -s $schema
╭───┬───┬─────╮
│ # │ a │ b │
├───┼───┼─────┤
│ 0 │ 1 │ foo │
│ 1 │ 2 │ bar │
╰───┴───┴─────╯