title: How to compute the difference in monthly income for the same id
body: <p>The dataframe below shows the monthly revenue of two shops (<code>shop_id=11</code>, <code>shop_id=15</code>) during the period of a few years:</p>
  
  <pre><code>data = { 'shop_id' : [   11,    15,    15,    15,    11,   11 ],
           'month'   : [    1,     1,     2,     3,     2,    3 ],
           'year'    : [ 2011,  2015,  2015,  2015,  2014, 2014 ],
           'revenue' : [11000,  5000,  4500,  5500, 10000, 8000]
         }
  
  df = pd.DataFrame(data)
  df = df[['shop_id', 'month', 'year', 'revenue']]
  display(df)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SwkTr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SwkTr.png" alt="enter image description here"></a></p>
  
  <p>You can notice that <code>shop_id=11</code> has only one entry in 2011 (january) and <code>shop_id=15</code> has a few entries in 2015 (january, february, march). Nevertheless, it's interesting to note that the first shop has a few more entries in 2014:</p>
  
  <p><a href="https://i.stack.imgur.com/TGJwo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TGJwo.png" alt="enter image description here"></a></p>
  
  <p>I'm trying to optimize a custom function (used along with <code>.apply()</code>) that creates a new feature called <code>diff_revenue</code>: this feature shows the change in revenue from the previous month, for each shop:</p>
  
  <p><a href="https://i.stack.imgur.com/hpewE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hpewE.png" alt="enter image description here"></a></p>
  
  <p>I would like to offer some explanation on how some of the values found in <code>diff_revenue</code> were generated:</p>
  
  <ul>
  <li>The value first cell is <strong>0</strong> (red) because there is no previous information for <code>shop_id=11</code>;</li>
  <li>The 2nd cell is also <strong>0</strong> (orange), for the same reason: there is no previous information for <code>shop_id=15</code>;</li>
  <li>The 3rd cell is <strong>500</strong> (green), because the change from the last entry (january, 2015) of this shop to the current cell's revenue (february, 2015), is 500 Trumps.</li>
  <li>The 5th cell is <strong>1000</strong> (dark blue), because the change from the last entry (january, 2011) of this shop to the current cell's revenue (february, 2014) was 1000 Trumps.</li>
  </ul>
  
  <p>I'm no expert in Pandas and was wondering if the Pandas' gods knew a better way. The DataFrame I have to work with is quite large (+1M observations) and my current approach is too slow. I'm looking for a faster alternative or maybe something more readable.</p>