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

[Python] Repeat rows in a Polars DataFrame based on column value

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 07:42.

  1. Stack

    Stack Membro Participativo

    I would like to expand the following Polars dataframe by repeating rows based on values in the quantity column.

    Original DataFrame:

    df = pl.from_repr("""
    ┌────────┬──────────┐
    │ Fruit ┆ Quantity │
    │ --- ┆ --- │
    │ str ┆ i64 │
    ╞════════╪══════════╡
    │ Apple ┆ 2 │
    │ Banana ┆ 3 │
    └────────┴──────────┘
    """)


    Expected Output:

    shape: (5, 3)
    ┌─────┬────────┬──────────┐
    │ ID ┆ Fruit ┆ Quantity │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ str ┆ i32 │
    ╞═════╪════════╪══════════╡
    │ 100 ┆ Apple ┆ 1 │
    │ 100 ┆ Apple ┆ 1 │
    │ 200 ┆ Banana ┆ 1 │
    │ 200 ┆ Banana ┆ 1 │
    │ 200 ┆ Banana ┆ 1 │
    └─────┴────────┴──────────┘


    Here is a very similar question but using Pandas instead of Polars: Repeat rows in a pandas DataFrame based on column value

    The polars repeat function does not seem to offer the same functionality as its Pandas counterpart: https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.repeat.html#polars.repeat

    Continue reading...

Compartilhe esta Página