A sequence of tuples where each tuple represents a when and then branch. This function accepts three input forms (see examples below). Each tuple is evaluated in order from top to bottom. For each tuple, the expressions before the final element are treated as when conditions and combined with &. If the combined condition evaluates to True, the corresponding then expression (the last element) is returned and the evaluation stops. If no condition matches any tuple, the otherwise expression is used as the fallback.
otherwise:pl.Expr | None=None
Fallback expression used when no conditions match.
Returns
:pl.Expr
A single Polars expression suitable for use in transformations.
Examples
DataFrame Context
The example below demonstrates all three supported input forms.
expr1 uses the simplest form, where each tuple contains a single when condition followed by its corresponding then expression.
expr2 shows tuples with multiple when conditions listed before the final then expression. These conditions are implicitly combined with &.
expr3 uses a tuple as the first element of each tuple, containing multiple when conditions which are also combined with & before evaluation.
In the list namespace, it may be easier to think of each row as an element in a list. Conceptually, you’re working with a pl.Series, where each row corresponds to one item in the list.
Check whether each string in the list starts with the letter “a” or “A”: