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 when for expression

Creates and modifies a when expression.

This command requires a plugin

The polars when 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 when {flags} (when expression) (then expression)

Parameters

  • when expression: when expression used for matching
  • then expression: expression that will be applied when predicate is true

Input/output types:

inputoutput
nothingexpression
expressionexpression
anyexpression

Examples

Create a when conditions

> polars when ((polars col a) > 2) 4

Create a when conditions

> polars when ((polars col a) > 2) 4 | polars when ((polars col a) < 0) 6

Create a new column for the dataframe

> [[a b]; [6 2] [1 4] [4 1]]
   | polars into-lazy
   | polars with-column (
    polars when ((polars col a) > 2) 4 | polars otherwise 5 | polars as c
     )
   | polars with-column (
    polars when ((polars col a) > 5) 10 | polars when ((polars col a) < 2) 6 | polars otherwise 0 | polars as d
     )
   | polars collect
╭───┬───┬───┬───┬────╮
│ # │ a │ b │ c │ d  │
├───┼───┼───┼───┼────┤
│ 0 │ 6 │ 2 │ 4 │ 10 │
│ 1 │ 1 │ 4 │ 5 │  6 │
│ 2 │ 4 │ 1 │ 4 │  0 │
╰───┴───┴───┴───┴────╯