[Python] Determine if value exists in previous rows

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to do this:

    import polars as pl

    df = pl.DataFrame({
    'j': [1, 2, 3, 4],
    'k': [3, 1, 2, 2],
    })

    df = df.with_row_index().with_columns([
    pl.struct(['index', 'j']).map_elements(
    lambda x: df.slice(0, x['index'])['k']
    .to_list().count(x['j']) > 0,
    pl.Boolean
    ).alias('l')
    ])

    print(df)

    shape: (4, 4)
    ┌───────┬─────┬─────┬───────┐
    │ index ┆ j ┆ k ┆ l │
    │ --- ┆ --- ┆ --- ┆ --- │
    │ u32 ┆ i64 ┆ i64 ┆ bool │
    ╞═══════╪═════╪═════╪═══════╡
    │ 0 ┆ 1 ┆ 3 ┆ false │
    │ 1 ┆ 2 ┆ 1 ┆ false │
    │ 2 ┆ 3 ┆ 2 ┆ true │
    │ 3 ┆ 4 ┆ 2 ┆ false │
    └───────┴─────┴─────┴───────┘


    where each row in l is true if the value in j has been seen in previous rows in k.

    Is there a way to do this without escaping into python via map_elements?

    Continue reading...

Compartilhe esta Página