简体   繁体   中英

Pandas rolling mean with variable window based on an different column

I would like to perform the rolling mean on a window that varies depending on the values of a column in my DataFrame. Can anyone help me? Here is a starting point:

import pandas as pd
import numpy as np

rng = np.random.default_rng()
df = pd.DataFrame(rng.integers(0, 100, size=(100, 2)), columns=list('AB'))
df.loc[:,'B']=df['B']//10

Now I would like to get the rolling mean of the series df.A with the window based on column B. For example if df.B[0] is worth 3 my_series[0]=df.A.rolling(3).mean()[0] and so on for my_series[1] etc...

Can you help me? ty for your time I appreciate it.

One option is to loop through the data frame, and assign a new column equal to the rolling_mean for each row.

df['rolling_mean'] = np.nan
for ind in range(len(df)):
    df.loc[df.index[ind], 'rolling_mean'] = df.A.rolling(df.loc[df.index[ind], 'B']).mean()[ind]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM