title: Selecting rows from a NumPy ndarray
body: <p>I want to select only certain rows from a <a href="http://en.wikipedia.org/wiki/NumPy" rel="noreferrer">NumPy</a> array based on the value in the second column. For example, this test array has integers from 1 to 10 in the second column.</p>
  
  <pre><code>&gt;&gt;&gt; test = numpy.array([numpy.arange(100), numpy.random.randint(1, 11, 100)]).transpose()
  &gt;&gt;&gt; test[:10, :]
  array([[ 0,  6],
         [ 1,  7],
         [ 2, 10],
         [ 3,  4],
         [ 4,  1],
         [ 5, 10],
         [ 6,  6],
         [ 7,  4],
         [ 8,  6],
         [ 9,  7]])
  </code></pre>
  
  <p>If I wanted only rows where the second value is 4, it is easy:</p>
  
  <pre><code>&gt;&gt;&gt; test[test[:, 1] == 4]
  array([[ 3,  4],
         [ 7,  4],
         [16,  4],
         ...
         [81,  4],
         [83,  4],
         [88,  4]])
  </code></pre>
  
  <p>But how do I achieve the same result when there is more than one wanted value?</p>
  
  <p>The wanted list can be of arbitrary length. For example, I may want all rows where the second column is either 2, 4 or 6:</p>
  
  <pre><code>&gt;&gt;&gt; wanted = [2, 4, 6]
  </code></pre>
  
  <p>The only way I have come up with is to use list comprehension and then convert this back into an array and seems too convoluted, although it works:</p>
  
  <pre><code>&gt;&gt;&gt; test[numpy.array([test[x, 1] in wanted for x in range(len(test))])]
  array([[ 0,  6],
         [ 3,  4],
         [ 6,  6],
         ...
         [90,  2],
         [91,  6],
         [92,  2]])
  </code></pre>
  
  <p>Is there a better way to do this in NumPy itself that I am missing?</p>