[Python] Polars str.starts_with() with values from another column

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

  1. Stack

    Stack Membro Participativo

    I have a polars DataFrame for example:

    >>> df = pl.DataFrame({'A': ['a', 'b', 'c', 'd'], 'B': ['app', 'nop', 'cap', 'tab']})
    >>> df
    shape: (4, 2)
    ┌─────┬─────┐
    │ A ┆ B │
    │ --- ┆ --- │
    │ str ┆ str │
    ╞═════╪═════╡
    │ a ┆ app │
    │ b ┆ nop │
    │ c ┆ cap │
    │ d ┆ tab │
    └─────┴─────┘


    I'm trying to get a third column C which is True if strings in column B starts with the strings in column A of the same row, otherwise, False. So in the case above, I'd expect:

    ┌─────┬─────┬───────┐
    │ A ┆ B ┆ C │
    │ --- ┆ --- ┆ --- │
    │ str ┆ str ┆ bool │
    ╞═════╪═════╪═══════╡
    │ a ┆ app ┆ true │
    │ b ┆ nop ┆ false │
    │ c ┆ cap ┆ true │
    │ d ┆ tab ┆ false │
    └─────┴─────┴───────┘


    I'm aware of the df['B'].str.starts_with() function but passing in a column yielded:

    >>> df['B'].str.starts_with(pl.col('A'))
    ... # Some stuff here.
    TypeError: argument 'sub': 'Expr' object cannot be converted to 'PyString'


    What's the way to do this? In pandas, you would do:

    df.apply(lambda d: d['B'].startswith(d['A']), axis=1)

    Continue reading...

Compartilhe esta Página