[Python] How to select the longest string from a list of strings in polars?

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

  1. Stack

    Stack Membro Participativo

    How do I select the longest string from a list of strings in polars?

    Example and expected output:

    import polars as pl

    df = pl.DataFrame({
    "values": [
    ["the", "quickest", "brown", "fox"],
    ["jumps", "over", "the", "lazy", "dog"],
    []
    ]
    })


    ┌──────────────────────────────┬────────────────┐
    │ values ┆ longest_string │
    │ --- ┆ --- │
    │ list[str] ┆ str │
    ╞══════════════════════════════╪════════════════╡
    │ ["the", "quickest", … "fox"] ┆ quickest │
    │ ["jumps", "over", … "dog"] ┆ jumps │
    │ [] ┆ null │
    └──────────────────────────────┴────────────────┘


    My use case is to select the longest overlapping match.

    Continue reading...

Compartilhe esta Página