What is the recommended way for retrieving row numbers (index) for polars?

Viewed 147

I know polars does not support index by design, so df.filter(expr).index isn't an option, another way I can think of is by adding a new column before applying any filters, not sure if this is an optimal way for doing so in polars

df.with_column(pl.Series('index', range(len(df))).filter(expr).index
1 Answers

Use with_row_count():

In [18]: df = pl.DataFrame([pl.Series("a", [5, 9, 6]), pl.Series("b", [8, 3, 4])])

In [19]: df
Out[19]: 
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 5   ┆ 8   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 9   ┆ 3   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 6   ┆ 4   │
└─────┴─────┘

In [20]: df.with_row_count()
Out[20]: 
shape: (3, 3)
┌────────┬─────┬─────┐
│ row_nr ┆ a   ┆ b   │
│ ---    ┆ --- ┆ --- │
│ u32    ┆ i64 ┆ i64 │
╞════════╪═════╪═════╡
│ 0      ┆ 5   ┆ 8   │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 1      ┆ 9   ┆ 3   │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2      ┆ 6   ┆ 4   │
└────────┴─────┴─────┘

# Start from 1 instead of 0.
In [21]: df.with_row_count(offset=1)
Out[21]: 
shape: (3, 3)
┌────────┬─────┬─────┐
│ row_nr ┆ a   ┆ b   │
│ ---    ┆ --- ┆ --- │
│ u32    ┆ i64 ┆ i64 │
╞════════╪═════╪═════╡
│ 1      ┆ 5   ┆ 8   │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2      ┆ 9   ┆ 3   │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 3      ┆ 6   ┆ 4   │
└────────┴─────┴─────┘

# Start from 1 and call column "my_index".
In [22]: df.with_row_count(name="my_index", offset=1)
Out[22]: 
shape: (3, 3)
┌──────────┬─────┬─────┐
│ my_index ┆ a   ┆ b   │
│ ---      ┆ --- ┆ --- │
│ u32      ┆ i64 ┆ i64 │
╞══════════╪═════╪═════╡
│ 1        ┆ 5   ┆ 8   │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2        ┆ 9   ┆ 3   │
├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 3        ┆ 6   ┆ 4   │
└──────────┴─────┴─────┘
Related