polars selector by-index for expression
Select columns by their index position. Supports negative indices (e.g., -1 for the last column).
Signature
> polars selector by-index {flags} ...rest
Flags
--not-strict: Allow out-of-range indices without error.
Parameters
...rest: Column index positions to select (negative indices count from the end).
Input/output types:
| input | output |
|---|---|
| any | polars_selector |
Examples
Select first and third columns by index
> [[a b c]; [1 2 3] [4 5 6]]
| polars into-df
| polars select (polars selector by-index 0 2)
| polars collect
╭───┬───┬───╮
│ # │ a │ c │
├───┼───┼───┤
│ 0 │ 1 │ 3 │
│ 1 │ 4 │ 6 │
╰───┴───┴───╯Select the last column using a negative index
> [[a b c]; [1 2 3] [4 5 6]]
| polars into-df
| polars select (polars selector by-index -1)
| polars collect
╭───┬───╮
│ # │ c │
├───┼───┤
│ 0 │ 3 │
│ 1 │ 6 │
╰───┴───╯