rolling call on one column and groupby second pandas 1

rolling call on one column and groupby second pandas

df['rolling_mean'] = df.groupby('id')['value'].transform(lambda x: x.rolling(2).mean())

Here is what the above code is Doing:
1. Grouping by ‘id’
2. Applying a rolling mean with a window of 2
3. Assigning the result to a new column called ‘rolling_mean’

Similar Posts