title: Finding most recent values of an element in a pandas dataframe
body: <p>I have a dataframe like this</p>
  
  <pre><code>HOME    AWAY    COUNT_H    COUNT_A    N_H     N_A
  Wild    Sharks    1          1        0.5    -0.4
  Jets    Sabres    1          1        0.7     0.3
  Sharks  Wild      2          2        0.2    -0.1
  Jets    Stars     2          1       -0.2     0.4
  Stars   Wild      1          3        0.7     0.3
  Sharks  Sabres    3          2        0.5    -0.4
  </code></pre>
  
  <p>I need to create two new columns 'K_H' and 'K_A' which show what the 'N_H' and 'N_A' were when the count was one number less than what the current row indicates.</p>
  
  <p>For example, my desired output is this:</p>
  
  <pre><code>HOME    AWAY    COUNT_H    COUNT_A    N_H     N_A    K_H     K_A
  Wild    Sharks    1          1        0.5    -0.4    n/a     n/a
  Jets    Sabres    1          1        0.7     0.3    n/a     n/a
  Sharks  Wild      2          2        0.2    -0.1    -0.4    0.5
  Jets    Stars     2          1       -0.2     0.4    0.7     n/a
  Stars   Wild      2          3        0.7     0.3    0.4    -0.1
  Sharks  Sabres    3          2        0.5    -0.4    0.2     0.3
  </code></pre>
  
  <p>I'm quite lost here. I've attempted to slice the dataframe whilst creating the new columns, but this isn't working. I've also made a for loop but it's unbearably slow. I can do it pretty easily in Excel using INDEX(MATCH()) functions, but I really need something for pandas.</p>
  
  <p>Any help is appreciated</p>
  
  <p>Thanks</p>