I have a dataframe with the following structure:
import numpy as np
import pandas as pd
df = pd.DataFrame(
{
"date": ["2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04"] * 2,
"group": ["A", "A", "A", "A", "B", "B", "B", "B"],
"x": [1, 2, 2, 3, 2, 3, 4, 2],
"condition": [1, 0, 1, 0] * 2
}
)
df
I want to calculate, the rolling moving average of the last 3 days of the column x:
- Per group
- Using only past data (not using the current row)
- Using only data for the rolling average where
condition = 1.
The outcome should be the following:
How can I do that in pandas? Thanks!
Keep in mind it is not the same as this:
Rolling function in pandas with condition
In here I'm looking for the moving average of the last 3 days, in the other one I just wanted the rolling average.
