Creating Grouped Rolling Features in Pandas

Python

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())
Did you find this snippet useful?

Sign up for free to to add this to your code library