[Python] Python-Polars: Create pairwise elements of two lists

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

  1. Stack

    Stack Membro Participativo

    In Python Polars I am trying to "group" two lists together, for example in a Pl.Struct, removing the group when one of the elements is Null. For example, given this DataFrame:

    df = pl.DataFrame({
    "Movie": [[None, "IT",]],
    "Count": [[30, 27]],
    })

    shape: (1, 2)
    ┌──────────────┬───────────┐
    │ Movie ┆ Count │
    │ --- ┆ --- │
    │ list[str] ┆ list[i64] │
    ╞══════════════╪═══════════╡
    │ [null, "IT"] ┆ [30, 27] │
    └──────────────┴───────────┘


    I would like to get the result below:


    shape: (1, 1)
    ┌───────────┐
    │ data │
    │ --- │
    │ struct[2] │
    ╞═══════════╡
    │ {"IT",27} │
    └───────────┘



    However, the challenge I am finding in this case is that I need to do it in a single context (i.e. in a single with_columns). Is it possible?

    Continue reading...

Compartilhe esta Página