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

[Python] How to take head(n) and tail(n) in one group_by with polars

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

  1. Stack

    Stack Membro Participativo

    Here is a sample dataframe, I want to get head(n) rows and tail(n) rows in everyday which is a group_by("date").agg() with polars. I know it could use two group_by to get head and tail part and then concat them.

    df = pl.from_repr("""
    ┌────────────┬────────┬──────────────────┐
    │ date ┆ symbol ┆ ts_dom2secdom_op │
    │ --- ┆ --- ┆ --- │
    │ date ┆ str ┆ f64 │
    ╞════════════╪════════╪══════════════════╡
    │ 2000-01-04 ┆ AL ┆ -0.119165 │
    │ 2000-01-04 ┆ RU ┆ 0.256691 │
    │ 2000-01-05 ┆ AL ┆ -0.126549 │
    │ 2000-01-05 ┆ RU ┆ 0.1851 │
    │ 2000-01-06 ┆ CU ┆ -0.121354 │
    │ 2000-01-06 ┆ RU ┆ 0.228452 │
    │ 2000-01-07 ┆ AL ┆ -0.126013 │
    │ 2000-01-07 ┆ RU ┆ 0.348729 │
    │ 2000-01-10 ┆ AL ┆ -0.139447 │
    │ 2000-01-10 ┆ RU ┆ 0.263048 │
    └────────────┴────────┴──────────────────┘
    """)


    Is there some trick to do this in one group_by or with_columns or filter?

    Continue reading...

Compartilhe esta Página