Spec-Zone.ru › Nushell

polars replace for expression

Create an expression that replaces old values with new values

Signature

> polars replace {flags} (old) (new)

Flags

  • --strict, -s: Require that all values must be replaced or throw an error (ignored if old or new are expressions).
  • --default, -d {any}: Set values that were not replaced to this value. If no default is specified, (default), an error is raised if any values were not replaced. Accepts expression input. Non-expression inputs are parsed as literals.
  • --return-dtype, -t {string}: Data type of the resulting expression. If set to null (default), the data type is determined automatically based on the other inputs.

Parameters

  • old: Values to be replaced.
  • new: Values to replace by.

Input/output types:

input output
polars_expression polars_expression

Examples

Replace column with different values of same type

> [[a]; [1] [1] [2] [2]]
                | polars into-df
                | polars select (polars col a | polars replace [1 2] [10 20])
                | polars collect
╭───┬────╮
│ # │ a  │
├───┼────┤
│ 0 │ 10 │
│ 1 │ 10 │
│ 2 │ 20 │
│ 3 │ 20 │
╰───┴────╯

Replace column with different values of another type

> [[a]; [1] [1] [2] [2]]
                | polars into-df
                | polars select (polars col a | polars replace [1 2] [a b] --strict)
                | polars collect
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ a │
│ 1 │ a │
│ 2 │ b │
│ 3 │ b │
╰───┴───╯

Replace column with different values based on expressions (cannot be used with strict)

> [[a]; [1] [1] [2] [2]]
                | polars into-df
                | polars select (polars col a | polars replace [(polars col a | polars max)] [(polars col a | polars max | $in + 5)])
                | polars collect
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ 1 │
│ 1 │ 1 │
│ 2 │ 7 │
│ 3 │ 7 │
╰───┴───╯

Replace column with different values based on expressions with default

> [[a]; [1] [1] [2] [3]]
                | polars into-df
                | polars select (polars col a | polars replace [1] [10] --default (polars col a | polars max | $in * 100) --strict)
                | polars collect
╭───┬─────╮
│ # │  a  │
├───┼─────┤
│ 0 │  10 │
│ 1 │  10 │
│ 2 │ 300 │
│ 3 │ 300 │
╰───┴─────╯

Replace column with different values based on expressions with default

> [[a]; [1] [1] [2] [3]]
                | polars into-df
                | polars select (polars col a | polars replace [1] [10] --default (polars col a | polars max | $in * 100) --strict --return-dtype str)
                | polars collect
╭───┬─────╮
│ # │  a  │
├───┼─────┤
│ 0 │ 10  │
│ 1 │ 10  │
│ 2 │ 300 │
│ 3 │ 300 │
╰───┴─────╯

Replace column with different values using a record

> [[a]; [1] [1] [2] [2]]
                | polars into-df
                | polars select (polars col a | polars replace {1: a, 2: b} --strict --return-dtype str)
                | polars collect
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ a │
│ 1 │ a │
│ 2 │ b │
│ 3 │ b │
╰───┴───╯

Copyright © 2019–2026 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/polars_replace.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API