title: Row wise subtraction based on multiple criteria (columns) in Python
body: <p>I have the following data:</p>
  
  <pre><code>date      locid month       price
  11/2/2017   17  11/1/2017   -0.0025
  11/2/2017   17  12/1/2017   0
  11/2/2017   105 11/1/2017   -0.895
  11/2/2017   105 12/1/2017   -1
  11/3/2017   17  11/1/2017   -0.0025
  11/3/2017   17  12/1/2017   0.01
  11/3/2017   105 11/1/2017   -0.895
  11/3/2017   105 12/1/2017   -1.01
  </code></pre>
  
  <p>I would like to return a result that shows the difference in prices, at each locid, for each month, on each date.  The result would only have four rows:</p>
  
  <pre><code>date        month       price
  11/2/2017   11/1/2017   0.8925
  11/2/2017   12/1/2017   1
  11/3/2017   11/1/2017   0.8925
  11/3/2017   12/1/2017   1.02
  </code></pre>
  
  <p>I've obtained the result only with the following line:</p>
  
  <pre><code>df.loc[df['locid']==17].price - df.loc[df['locid']==105].price
  </code></pre>
  
  <p>However, I don't think this is a robust solution. Prior to sorting the df.date column, my result contained eight rows. Also, i'm not sure that python/pandas is matching the df.months, I think they just may happen to be in the right order.</p>
  
  <p>I would like to know how to:</p>
  
  <p>1) <strong>ensure i am subtracting in the right order (in this case i want locid 17-105), what if they were in the opposite order in my data</strong>?  What if there were three locids and i wanted to see only two?</p>
  
  <p>2) ensure locid 17-105 is date==11/2/2017 and month==11/1/2017.  IE date and month have to match, before subtracting</p>
  
  <p>3) if the data was pivoted first, how to do the subtraction</p>
  
  <p>Thank you</p>