Search code examples
pythonpython-polars

Python Polars - Create sequence in list from integer


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:

enter image description here

To this:

enter image description here

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')
)

Solution

  • pl.int_ranges()

    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] │
    └─────┴───────────┘