title: lags with calculation and groupby a column
body: <p>I have a dataframe below. I am trying to create lags for var1,var2, var3 by calculating
  (var_n/ lag2(var_n))-2  (where n is 1,2,3)</p>
  <p>below code works fine for lag2. But I need to perform the calculation grouped by &quot;grp&quot;</p>
  <p>CODE:</p>
  <p>lag=[2]</p>
  <p>df=pd.concat([df]+[df.groupby('grp'['var1','var2','var3'].shift(x).add_prefix('lag'+str(x)) for x in lag],axis=1)</p>
  <p>In a different approach I tried below but I am not able to apply group by:</p>
  <p>yoy = [12]</p>
  <p>columns_y = df.loc[:, 'var1':'var3']</p>
  <p>for col in columns_y.columns:</p>
  <pre><code>for x in yoy: 
      columns_y.loc[:,col+&quot;_yoy&quot;] =(columns_y[col]/(columns_y[col].shift(x)))-1
  </code></pre>
  <p><a href="https://i.stack.imgur.com/0hbd3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0hbd3.png" alt="enter image description here" /></a></p>