[Python] How to recursively calculate a column based on previous row values of another column...

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to calculate a weighted_goals column in a DataFrame using the Polars library in Python. I want each row's weighted_goals your textvalue to depend on previous values of a column called goals, calculated for each team using a group-based window (over("Team")).

    The formula I’m aiming to implement is something like this:

    • For each row:

    weighted_goals = 0.8 * goals[n] + 0.2 * (0.8 * goals[n-1] + 0.2 * (0.8 * goals[n-2] ...)

    The idea is for weighted_goals to be a recursive calculation that takes into account previous values in goals for each team, with an exponential decay (or weighting) at each step.

    To illustrate, suppose I have the following DataFrame:

    import polars as pl

    data = pl.DataFrame({
    "Team": ["A", "A", "A", "B", "B", "B"],
    "Week": [1, 2, 3, 1, 2, 3],
    "goals": [2, 1, 3, 1, 4, 2]
    })


    For this input, I would like the weighted_goals column to look something like this (using the recursive formula mentioned above):

    Team Week goals weighted_goals
    A 1 2 2
    A 2 1 0.8 * 1 + 0.2 * 2 = 1.2
    A 3 3 0.8 * 3 + 0.2 * (0.8 * 1 + 0.2 * 2) = 2.56
    B 1 1 1
    B 2 4 0.8 * 4 + 0.2 * 1 = 3.2
    B 3 2 0.8 * 2 + 0.2 * (0.8 * 4 + 0.2 * 1) = 2.56

    How can I implement this recursive formula in Polars? Is there an efficient way to achieve this without using an explicit loop?

    Continue reading...

Compartilhe esta Página