polars.row_index#
- polars.row_index(name: str = 'index') Expr [source]#
Generates a sequence of integers.
The length of the returned sequence will match the context length, and the datatype will match the one returned by
get_index_dtype()
.Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
If you would like to generate sequences with custom offsets / length / step size / datatypes, it is recommended to use
int_range
instead.- Parameters:
- name
Name of the returned column.
- Returns:
- Expr
Column of integers.
See also
int_range
Generate a range of integers.
Examples
>>> df = pl.DataFrame({"x": ["A", "A", "B", "B", "B"]}) >>> df.with_columns(pl.row_index(), pl.row_index("another_index")) shape: (5, 3) ┌─────┬───────┬───────────────┐ │ x ┆ index ┆ another_index │ │ --- ┆ --- ┆ --- │ │ str ┆ u32 ┆ u32 │ ╞═════╪═══════╪═══════════════╡ │ A ┆ 0 ┆ 0 │ │ A ┆ 1 ┆ 1 │ │ B ┆ 2 ┆ 2 │ │ B ┆ 3 ┆ 3 │ │ B ┆ 4 ┆ 4 │ └─────┴───────┴───────────────┘ >>> df.group_by("x").agg(pl.row_index()).sort("x") shape: (2, 2) ┌─────┬───────────┐ │ x ┆ index │ │ --- ┆ --- │ │ str ┆ list[u32] │ ╞═════╪═══════════╡ │ A ┆ [0, 1] │ │ B ┆ [0, 1, 2] │ └─────┴───────────┘ >>> df.select(pl.row_index()) shape: (5, 1) ┌───────┐ │ index │ │ --- │ │ u32 │ ╞═══════╡ │ 0 │ │ 1 │ │ 2 │ │ 3 │ │ 4 │ └───────┘