title: Getting a previous value Pandas
body: <p>I'm working on feature extraction for a Machine Learning model and for every row I need to compare the current price to the previous price. To do this I sort my dataframe by my datetime column, iterate over the rows and keep a dictionary with the product ID as key and the last price as value. My dataset is very big, around 5m 'sales' in my training set and also in my test set. Even on a small sample (about 250k products) it is taking a very long time and a lot of memory. I've used as much vectorizing functions throughout other portions of my code but I don't know how I can make this part more efficient. Here's what I'm doing right now:</p>
  
  <pre><code>data = data.sort_values('date_time')
  previous_price = {}
  data_list = []
  for index, value in data.iterrows():
      if value['prop_id'] in previous_price.keys():
          data_list.append(value['price_usd']-previous_price[value['prop_id']])
      else:
          data_list.append(0)
      previous_price[value['prop_id']] = value['price_usd']
  data['previous_price_diff'] = data_list
  </code></pre>