applying a mask on a nested numpy array - numpy - python
<p>a bit embarassing to ask since the heavy documentation on Numpy but I was stuck doing this simple task, that is getting all the records for which a mask is true in a nested numpy representation (equivalent to the <code>dataframe.loc[cond]</code> in <code>pandas</code>):</p>

<pre><code>import numpy as np
a1 = np.array([1,2,3])
a2 = np.array(['a','b','c'])
a3 = np.array(['luca','paolo','francesco'])
a4 = np.array([True, False,False], dtype='bool')

combination = np.array([a1,a2,a3,a4])
print(combination)

# slice for a4 == True 
combination[combination[3] == 'True']
</code></pre>

<p>but the result is not what I want.</p>

<p>in fact from <code>combination</code> :</p>

<pre><code>[['1' '2' '3']
 ['a' 'b' 'c']
 ['luca' 'paolo' 'francesco']
 ['True' 'False' 'False']]
</code></pre>

<p>it yields with <code>combination[combination[3] == 'True']</code>:</p>

<pre><code>array([['1', '2', '3']], 
      dtype='&lt;U11')
</code></pre>

<p>when in reality I want:</p>

<pre><code>[['1']
 ['a' ]
 ['luca']
 ['True' ]]
</code></pre>

<p>any ideas on what I am doing wrong?</p>

<p>P.S.: no i can't do it in pandas because pandas has my RAM exploding when converting this to a <code>pandas.Dataframe</code>  </p>

<p>I believe you're simply missing the indexes of the other dimension:</p>

<pre><code>combination[combination[3] == 'True']
</code></pre>

<p>should be</p>

<pre><code>combination[:, combination[3] == 'True']
</code></pre>

<p>Note the colon.</p>

<p>This yields a new ndarray indexed over all of the first dimension and only 0 in the second.</p>

python arrays numpy indexing slice