[Python] Polars join on array items without explode/groupby

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    a follow up from Polars lazyframe - add fields from other lazyframe as struct without a `collect`.

    I now want to join on array items. Currently the only way i know of doing this would be to first explode the array, perform the join, do a groupby, then collect back as a list. I'm hoping there is a more concise alternative.

    companies = pl.DataFrame({
    "id": [1],
    "name": ["google"],
    "industry": [1001]
    }).lazy()

    industries = pl.DataFrame({
    "id": [1001],
    "name": ["tech"],
    "sectors": [[10011, 10012]]
    }).lazy()

    sectors = pl.DataFrame({
    "id": [10011, 10012],
    "name": ["software", "hardware"],
    }).lazy()

    expected = pl.DataFrame({
    "id": [1],
    "name": ["polars"],
    "industry": [{
    "name": "tech",
    "sectors": [[{"name": "software"}, {"name": "hardware"}]]
    }]
    })

    Continue reading...

Compartilhe esta Página