1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Shorter / better way to simulate dropWhile in polars

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024 às 08:22.

  1. Stack

    Stack Membro Participativo

    Say I have this:

    >>> df = polars.DataFrame(dict(j=[1,1,1,2,1,2,1,2,2], k=[1,2,3,4,5,6,7,8,9]))
    >>> df
    shape: (9, 2)
    ┌─────┬─────┐
    │ j ┆ k │
    │ --- ┆ --- │
    │ i64 ┆ i64 │
    ╞═════╪═════╡
    │ 1 ┆ 1 │
    │ 1 ┆ 2 │
    │ 1 ┆ 3 │
    │ 2 ┆ 4 │
    │ 1 ┆ 5 │
    │ 2 ┆ 6 │
    │ 1 ┆ 7 │
    │ 2 ┆ 8 │
    │ 2 ┆ 9 │
    └─────┴─────┘


    Is there a shorter / better way to simulate dropWhile (i.e. remove all the rows at the top of the DataFrame that satisfy a certain condition) than this (which removes all initial rows that have j == 1):

    >>> df.slice(df.with_row_index().filter(polars.col('j') != 1).select('index').head(1).item())
    shape: (6, 2)
    ┌─────┬─────┐
    │ j ┆ k │
    │ --- ┆ --- │
    │ i64 ┆ i64 │
    ╞═════╪═════╡
    │ 2 ┆ 4 │
    │ 1 ┆ 5 │
    │ 2 ┆ 6 │
    │ 1 ┆ 7 │
    │ 2 ┆ 8 │
    │ 2 ┆ 9 │
    └─────┴─────┘

    Continue reading...

Compartilhe esta Página