title: What is a faster way to iterate through a numpy array in Python
body: <p>I am wondering if there is a better way to iterate through numpy arrays?
  I have timed my nested iterations and it takes roughly about 40-50 seconds per loop, and i am wondering if there is a faster way to do it? I know that looping through numpy arrays is not ideal, however I'm out of ideas. I looked through many questions on Stack Overflow but all of them ends up confusing me even more.</p>
  <p>I have tried converting the numpy array to a list using the <code>tolist()</code> function, however the run time is equally slower, if not worse.</p>
  <pre><code>def euc_distance(array1, array2):
      return np.power(np.sum((array1 - array2)**2) , 0.5)
  
  for i in range(N):
      for j,n in enumerate(data2.values): 
          distance = euc_distance(n, D[i]) 
          if distance &lt; Dradius[i] and NormAttListTest[j] == &quot;Attack&quot;:
              TP += 1
  </code></pre>
  <p>My euc_distance function passes in an array form (In my case, 5 dimensional) inputs, to output a 1 dimensional value. My <code>data2.values</code> is my way of access the numpy array through the pandas framework which is a [500 000, 5] dataframe.</p>
  <p>(Note that the NormAttListTest is a list that has the categorical data of &quot;Attack&quot; and &quot;Normal&quot; tagged to each individual testing data).</p>