[Python] How to compute a column in polars dataframe using np.linspace

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following pl.DataFrame:

    df = pl.DataFrame(
    data={
    "np_linspace_start": [0, 0, 0],
    "np_linspace_stop": [8, 6, 7],
    "np_linspace_num": [5, 4, 4]
    }
    )

    shape: (3, 3)
    ┌───────────────────┬──────────────────┬─────────────────┐
    │ np_linspace_start ┆ np_linspace_stop ┆ np_linspace_num │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 │
    ╞═══════════════════╪══════════════════╪═════════════════╡
    │ 0 ┆ 8 ┆ 5 │
    │ 0 ┆ 6 ┆ 4 │
    │ 0 ┆ 7 ┆ 4 │
    └───────────────────┴──────────────────┴─────────────────┘


    How can I create a new column ls, that is the result of the np.linspace function? This column will hold an np.array.

    I was looking for something along those lines:

    df.with_columns(
    ls=np.linspace(
    start=pl.col("np_linspace_start"),
    stop=pl.col("np_linspace_stop"),
    num=pl.col("np_linspace_num")
    )
    )


    Is there a polars equivalent to np.linspace?

    Continue reading...

Compartilhe esta Página