Creating Grouped Rolling Features in Pandas

Python
Time Series

In this code snippet we create features that give the rolling average, minimum and maximum stock prices over the previous 20 rows, grouped by stock.

 1|  df['avg_price_20'] = df.groupby(['stock'])['price'].transform(lambda x: x.rolling(20).mean())
 2|  
 3|  df['min_price_20'] = df.groupby(['stock'])['price'].transform(lambda x: x.rolling(20).min())
 4|  
 5|  df['max_price_20'] = df.groupby(['stock'])['price'].transform(lambda x: x.rolling(20).max())
1 Upvote
Tags: Rolling
Did you find this snippet useful?

Sign up to bookmark this in your snippet library