[Python] Python: How to use changing window in pandas rolling groupby function

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a DataFrame with monthly data that looks something like this:

    id date window_in_months value
    1 2000-01-01 3 20
    1 2000-02-01 3 30
    2 2000-01-01 12 40
    2 2000-02-01 12 60

    I want to do a rolling groupby but use the value in column window_in_months as the window parameter. I can use a static window by just doing:

    df.groupby('id')['value'].rolling(window=3, min_periods=3)

    But I can't figure out how to have the window and min_periods change with the group under consideration. The window_in_months will always be the same for all ids that are the same.

    Continue reading...

Compartilhe esta Página