Using pandas' group with offset

I want to use pd.rolling_mean

in groupby

. I want to have in each group a moving average of the previous items in the same group. Here's an example:

id    val
0     1
0     2
0     3
1     4
1     5
2     6

      

Grouping with id

, this should be converted to:

id    val
0     nan
0     1
0     1.5
1     nan
1     4
2     nan

      

+3


source to share


2 answers


I believe what you want pd.Series.expanding



df.groupby('id').val.apply(lambda x: x.expanding().mean().shift())

0    NaN
1    1.0
2    1.5
3    NaN
4    4.0
5    NaN
Name: val, dtype: float64

      

+2


source


It seems to me what you need groupby

with shift

and rolling

, the window size can be set to a scalar:

df['val']=df.groupby('id')['val'].apply(lambda x: x.shift().rolling(2, min_periods=1).mean())
print (df)
   id  val
0   0  NaN
1   0  1.0
2   0  1.5
3   1  NaN
4   1  4.0
5   2  NaN

      



Thanks 3novak

for the comment - you can set the window size to the maximum group length:

f = lambda x: x.shift().rolling(df['id'].value_counts().iloc[0], min_periods=1).mean()
df['val'] = df.groupby('id')['val'].apply(f)
print (df)
   id  val
0   0  NaN
1   0  1.0
2   0  1.5
3   1  NaN
4   1  4.0
5   2  NaN

      

+2


source







All Articles