[Python] Polars-Python: Compare list columns

Discussão em 'Python' iniciado por Stack, Novembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    In Python Polars, I have a dataframe like the below:

    df = pl.DataFrame(
    {"sets": [[1, 2, 3], [1, 2], [9, 10]], "optional_members": [[1, 2, 3], [1, 2], [9, 0]]}
    )

    shape: (2, 2)
    ┌───────────┬──────────────────┐
    │ sets ┆ optional_members │
    │ --- ┆ --- │
    │ list[i64] ┆ list[i64] │
    ╞═══════════╪══════════════════╡
    │ [1, 4, 3] ┆ [1, 2, 3] │
    │ [1, 0] ┆ [1, 2] │
    └───────────┴──────────────────┘


    I would like to build an expression that gets me the elements of the first column that are in the second, keeping the shape of the former, i.e:


    shape: (2, 3)
    ┌───────────┬──────────────────┬─────────────────────┐
    │ sets ┆ optional_members ┆ result │
    │ --- ┆ --- ┆ --- │
    │ list[i64] ┆ list[i64] ┆ list[bool] │
    ╞═══════════╪══════════════════╪═════════════════════╡
    │ [1, 4, 3] ┆ [1, 2, 3] ┆ [true, false, true] │
    │ [1, 0] ┆ [1, 2] ┆ [true, false] │
    └───────────┴──────────────────┴─────────────────────┘



    I have tried using eval over the first list, something like:


    func = lambda x, y: y.list.contains(x)

    df.with_columns(contains=
    pl.col("optional_members")
    .list.
    eval(func(pl.element(), pl.col("optional_members"))))



    But the pl.col() expression cannot be in an eval.

    How could we aaddress this while keeping the solution in a single expression?

    Thanks to @roman comment, a point need to be made: the check should be done regardless of the position.

    Continue reading...

Compartilhe esta Página