[Python] Python Polars match lists of patterns on every row

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I have a DataFrame of Strings, and another of Patterns.

    import polars as pl

    df_str = pl.DataFrame({
    "Strings": [
    "Apples are red",
    "Apples are not Bananas",
    "Bananas are not red",
    "I like Apples and Bananas",
    "Oranges are kinda red",
    ]
    })

    df_pat = pl.DataFrame({
    "Patterns": [
    ["Bananas", "red"],
    ["Apples"],
    ["Apples", "Bananas"]
    ]
    })


    I want to match every pattern in Patterns against every string in Strings. That the Patterns are given in list form means, that all words from the list have to be found in order to consider the list to be found. The result should therefore look something like this:

    shape: (5, 3)
    ┌───────────────────────────┬───────────────┬─────────────────────────────────────┐
    │ Strings ┆ matches_index ┆ matches │
    │ --- ┆ --- ┆ --- │
    │ str ┆ list[i64] ┆ list[list[str]] │
    ╞═══════════════════════════╪═══════════════╪═════════════════════════════════════╡
    │ Apples are red ┆ [1] ┆ [["Apples"]] │
    │ Apples are not Bananas ┆ [1, 2] ┆ [["Apples"], ["Apples", "Bananas"]] │
    │ Bananas are not red ┆ [0] ┆ [["Bananas", "red"]] │
    │ I like Apples and Bananas ┆ [1, 2] ┆ [["Apples"], ["Apples", "Bananas"]] │
    │ Oranges are kinda red ┆ [] ┆ [] │
    └───────────────────────────┴───────────────┴─────────────────────────────────────┘


    Right now my solution is to iterate over the patterns, concatenate each list with |, use extract_all for the concatenated pattern and check if the number of unique matches equals the length of the original pattern list. That works but maybe somebody has an idea how to implement this in native polars (without iter_rows() over the patterns).

    Thanks :)

    Continue reading...

Compartilhe esta Página