Considering that:
Using apply is strongly discouraged as you will be effectively running python “for” loops. This will be very slow. Wherever possible you should strongly prefer the native expression API to achieve the best performance.
How can I use Polars native expression API to generate a new column with a list containing all the integers from 1 to the number of another column?
This means, going from this:
To this:
Using apply:
# Import package.
import polars as pl
# Create the DataFrame.
df = pl.DataFrame({'Qty': [1, 2, 3]})
# Add the 'List' column.
df = df.with_columns(
pl.col('Qty').apply(lambda x: [i+1 for i in range(x)]).alias('List')
)
df.with_columns(list = pl.int_ranges(1, pl.col("Qty") + 1))
shape: (3, 2)
┌─────┬───────────┐
│ Qty ┆ list │
│ --- ┆ --- │
│ i64 ┆ list[i64] │
╞═════╪═══════════╡
│ 1 ┆ [1] │
│ 2 ┆ [1, 2] │
│ 3 ┆ [1, 2, 3] │
└─────┴───────────┘