title: How to index the second to last row of a numpy array-python
body: <p>I want to index the second to last row of a numpy array
  I have a loop that constantly appends new rows:  </p>
  
  <pre><code>arr2= is an array formatted like so [[300 240 22 22]] 
  </code></pre>
  
  <p>so in the example above x=300 y=240 w=22 h=22<br>
  these numbers get processed in the equation until wm has 2 numbers so [200, 300] and hm also has 2 numbers [200, 300] and are both NON-STOP appended to the end of the arr(not creating new arrays but rather adding to the single array. So it would look something like this constantly adding new rows(i don't know if it actually is adding new rows or overriding the old values):</p>
  
  <pre><code>----row [[301.77325 225.71806][366.08594 232.20958]]
  ----row [[301.54193 225.93848][365.76663 232.37358]]
  ---- [[300.9011  225.27153][365.21063 231.52242]]second to last appended 
  row 
  ---- [[300.63205 223.20387][364.91928 229.46263]]last appended row
  then there would be another row being appended this is just a snapshot
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>arr=[] #shape is (0,)
  for (x, y, w, h) in arr2:
      wm=int(x + (w/2))
      hm=int(y + (h/2))
      arr.append([wm, hm])
  
  simplified version:   
  arr=[]
  for (x, w) in arr2:
      wm=int(x + (w/2))
      arr.append([wm])
  </code></pre>
  
  <p>The non-simplified code produces an array with shape (2,2) with a dtype:float32.</p>
  
  <pre><code>column (wm) column(hm)
     |            |
     |            |
  [[293.51373 323.4329 ] # second to last row
   [247.77493 316.02783]]
  
  [[292.9887  322.23425]
   [247.24142 314.2921 ]]
  </code></pre>
  
  <p>These are some examples of the indexing methods I've tried(along with the array)(these are the two last appended rows of the array):</p>
  
  <pre><code>arr [[255.44836 280.92575] # I want to index this line-second to last appended row
       [298.6107  285.75986]]
  
  arr[-1] [298.6107  285.75986]
  arr[-2] [255.44836 280.92575]
  arr[-2][0] 255.44836
  arr[:,0] [255.44836 298.6107 ]
  arr[-1,:] [298.6107  285.75986]
  arr[-2,0] 255.44836
  arr[-2::2] [255.44836 280.92575] 
  
  arr [[255.35194 281.08353] # along with this line-last appended row
       [298.45673 285.88693]]
  
  arr[-1] [298.45673 285.88693]
  arr[-2] [255.35194 281.08353]
  arr[-2][0] 255.35194
  arr[:,0] [255.35194 298.45673]
  arr[-1,:] [298.45673 285.88693]
  arr[-2,0] 255.35194
  arr[-2::2] [255.35194 281.08353]
  </code></pre>
  
  <p>Other times when I try to index the second to last row it shows me an axis error, which could be the problem(that the appending doesn't add new rows which is what I am trying to index but rather overrides the old values). I don't know. So I tried to switch the appending method with: concatenate, vstack, pandas. But it didn't help.
  All I want to do is to find the difference between the last and second to last appended rows from the second column in the wm element (in the example 280.92575-281.08353) and also the same with the first column of wm element(255.44836-255.35194). All the various indexing methods have not fetched the second to last row just the last row. Please let me know if there's a different way to index the second to last row in a different way or if there are other indexing methods which I haven't already tried. Sorry if it is confusing still getting the hang of it. I can try to clear up any confusion. Thanks in advance!</p>