Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pandas' groupby with shifting

I am looking to use pd.rolling_mean in a groupby operation. I want to have in each group a rolling mean of the previous elemnets within the same group. Here is an example:

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

Grouping by id, this should be transformed into:

id    val
0     nan
0     1
0     1.5
1     nan
1     4
2     nan
like image 257
splinter Avatar asked Feb 04 '23 18:02

splinter


1 Answers

I believe 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
like image 113
piRSquared Avatar answered Feb 08 '23 07:02

piRSquared