Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- X2 X3 X4 Y Y1
- 01.02.2019 1 1 1
- 02.02.2019 2 2 0
- 02.02.2019 2 3 0
- 02.02.2019 2 1 1
- 03.02.2019 1 2 1
- 04.02.2019 2 3 0
- 05.02.2019 1 1 1
- 06.02.2019 2 2 0
- 07.02.2019 1 3 1
- 08.02.2019 2 1 1
- 09.02.2019 1 2 0
- 10.02.2019 2 3 1
- 11.02.2019 1 1 0
- 12.02.2019 2 2 1
- 13.02.2019 1 3 0
- 14.02.2019 2 1 1
- 15.02.2019 1 2 1
- 16.02.2019 2 3 0
- 17.02.2019 1 1 1
- 18.02.2019 2 2 0
- filtered_X4 = df.where(condition_1 & condition_2 & condition_3)
- df = filtered_X4.shift(1).rolling(window = 999999, min_periods = 1).mean()
- In [122]: df
- Out[122]:
- X2 X3 X4 Y
- 0 2019-02-01 1 1 9
- 1 2019-02-02 1 2 4
- 2 2019-02-02 1 1 3
- 3 2019-02-02 1 3 4
- 4 2019-02-03 1 2 3
- 5 2019-02-04 1 3 6
- 6 2019-02-05 1 4 1
- 7 2019-02-06 1 5 7
- 8 2019-02-07 1 6 6
- 9 2019-02-08 1 1 5
- def roll(df, win="5D", dt_col="X2", filter_cols=["X3","X4"],
- agg_func="mean", apply_col="Y", fill_value=None,
- verbose=0):
- data = []
- index = []
- for r in df.iterrows():
- mask = (df.index <= r[0]) & (df[dt_col] >= r[1][dt_col] - pd.to_timedelta(win))
- qry = " and ".join([f"{col} == {r[1][col]}" for col in filter_cols])
- index.append(r[0])
- data.append(df.loc[mask].query(qry)[apply_col].agg(agg_func))
- if verbose > 0:
- print(f"index: {r[0]}, avg: {data[-1]}", df.loc[mask].query(qry))
- res = pd.Series(data, index=index)
- if fill_value is not None:
- res = res.fillna(fill_value)
- return res
- In [128]: df["Y1"] = df.pipe(func=roll, win="5D", dt_col="X2", filter_cols=["X3","X4"],
- agg_func="mean", apply_col="Y")
- In [129]: df
- Out[129]:
- X2 X3 X4 Y Y1
- 0 2019-02-01 1 1 9 9.0
- 1 2019-02-02 1 2 6 6.0
- 2 2019-02-02 1 1 6 7.5 # <---
- 3 2019-02-02 1 3 7 7.0
- 4 2019-02-03 1 2 0 3.0 # <---
- 5 2019-02-04 1 3 7 7.0
- 6 2019-02-05 1 4 0 0.0
- 7 2019-02-06 1 5 6 6.0
- 8 2019-02-07 1 6 2 2.0
- 9 2019-02-08 1 1 7 7.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement