[Python] Python-polars: from coefficients, values and (nested) lists to weighted values

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    Let's say I've got a Polars DataFrame similar to this one:

    import polars as pl
    from decimal import Decimal

    df = pl.from_dicts(
    [
    {"id": 1, "value": Decimal("100.0"), "items": ["A"]},
    {"id": 2, "value": Decimal("150.000"), "items": ["A", "B"]},
    {"id": 3, "value": Decimal("70.0000"), "items": ["A", "B", "C"]},
    ]
    )

    id → pl.Int64 value → pl.Decimal items → pl.List(str)
    1 100 ["A"]
    2 150 ["A", "B"]
    3 70 ["A", "B", "C"]

    And the following Python dictionary:

    coef = {"A": Decimal("0.2"), "B": Decimal("0.35"), "C": Decimal("0.45") }


    From here, how to get the following DataFrame in an efficient manner using Polars?

    ┌─────┬────────────────┬──────┐
    │ id ┆ value ┆ item │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ decimal[*,10] ┆ str │
    ╞═════╪════════════════╪══════╡
    │ 1 ┆ 100.0000000000 ┆ A │
    │ 2 ┆ 54.5454000000 ┆ A │
    │ 2 ┆ 95.4544500000 ┆ B │
    │ 3 ┆ 14.0000000000 ┆ A │
    │ 3 ┆ 24.5000000000 ┆ B │
    │ 3 ┆ 31.5000000000 ┆ C │
    └─────┴────────────────┴──────┘


    In this example, 54.5454... for instance corresponds to 0.2 / (0.2 + 0.35) * 150.

    Note that the length of the coef dictionary isn't always the same. It basically looks like {key1: coef1, key2: coef2, ... } with n keys and n values (whose sum is equal to Decimal(1)).

    Also, I'm working with Decimal (not float64 values).

    Continue reading...

Compartilhe esta Página