0
let mut df = df! (
    "start" => &[6, 3, 1],
    "end" => &[7, 4, 2],
    "repeats" => &[1, 0, 2],
)?.sort(["start", "end"], false)?;

How do I repeat each row by the number given in the repeats column?

Here is an example attempt:

df.with_column(
    all().repeat_by(col("repeats")).explode()
);

It leads to the error

the trait `AsRef<str>` is not implemented for `Expr`
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • Note that this question is different as it uses python: https://stackoverflow.com/questions/73523306/repeat-rows-in-a-polars-dataframe-based-on-column-value – The Unfun Cat May 29 '23 at 11:11

1 Answers1

0
df.clone().lazy().select(
    [
        all().repeat_by(col("repeats")).explode().drop_nulls(),
    ]
);

If you want to repeat by another collection, use lit:

all().repeat_by(lit(_series))...
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156