Repeat rows in a Polars DataFrame based on column value
I would like to expand the following Polars dataframe by repeating rows based on values in the quantity column.
Original DataFrame:
import polars as pl
df = pl.from_repr("""
┌────────┬──────────┐
│ Fruit ┆ Quantity │
│ --- ┆ --- │
│ str ┆ i64 │
╞════════╪══════════╡
│ Apple ┆ 2 │
│ Banana ┆ 3 │
└────────┴──────────┘
""")
Expected Output:
shape: (5, 3)
┌─────┬────────┬──────────┐
│ ID ┆ Fruit ┆ Quantity │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i32 │
╞═════╪════════╪══════════╡
│ 100 ┆ Apple ┆ 1 │
│ 100 ┆ Apple ┆ 1 │
│ 200 ┆ Banana ┆ 1 │
│ 200 ┆ Banana ┆ 1 │
│ 200 ┆ Banana ┆ 1 │
└─────┴────────┴──────────┘
Here is a very similar question but using Pandas instead of Polars:
The polars repeat function does not seem to offer the same functionality as its Pandas counterpart: