polars list-contains
for dataframe
Checks if an element is contained in a list.
This command requires a plugin
The polars list-contains
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 list-contains {flags} (element)
Parameters
element
: Element to search for in the list
Input/output types:
input | output |
---|---|
any | any |
Examples
Returns boolean indicating if a literal element was found in a list column
> let df = [[a]; [[a,b,c]] [[b,c,d]] [[c,d,f]]] | polars into-df -s {a: list<str>};
let df2 = $df | polars with-column [(polars col a | polars list-contains (polars lit a) | polars as b)] | polars collect;
$df2.b
╭───┬───────╮
│ # │ b │
├───┼───────┤
│ 0 │ true │
│ 1 │ false │
│ 2 │ false │
╰───┴───────╯
Returns boolean indicating if an element from another column was found in a list column
> let df = [[a, b]; [[a,b,c], a] [[b,c,d], f] [[c,d,f], f]] | polars into-df -s {a: list<str>, b: str};
let df2 = $df | polars with-column [(polars col a | polars list-contains b | polars as c)] | polars collect;
$df2.c
╭───┬───────╮
│ # │ b │
├───┼───────┤
│ 0 │ true │
│ 1 │ false │
│ 2 │ true │
╰───┴───────╯
Returns boolean indicating if an element from another expression was found in a list column
> let df = [[a, b]; [[1,2,3], 4] [[2,4,1], 2] [[2,1,6], 3]] | polars into-df -s {a: list<i64>, b: i64};
let df2 = $df | polars with-column [(polars col a | polars list-contains ((polars col b) * 2) | polars as c)] | polars collect;
$df2.c
╭───┬───────╮
│ # │ b │
├───┼───────┤
│ 0 │ false │
│ 1 │ true │
│ 2 │ true │
╰───┴───────╯