Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mask numpy structured array on multiple columns?

I have a numpy structured array with a dtype such as:

A = numpy.empty(10, dtype=([('segment', '<i8'), ('material', '<i8'), ('rxN', '<i8')]))

I know I can create a mask such as:

A[A['segment'] == 42] = ...

Is there a way to create a mask on multiple columns? For example (I know this doesn't work, but I wish it did):

A[A['segment'] == 42 and A['material'] == 5] = ...
like image 916
jlconlin Avatar asked Jul 22 '11 15:07

jlconlin


People also ask

What is a masked NumPy array?

A masked array is the combination of a standard numpy. ndarray and a mask. A mask is either nomask , indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

What is Recarray?

recarray, which allows field access by attribute on the array object, and record arrays also use a special datatype, numpy. record, which allows field access by attribute on the individual elements of the array.


1 Answers

You can use the & operator instead of and:

A[(A['segment'] == 42) & (A['material'] == 5)]

Note that extra parantheses are required.

like image 131
Sven Marnach Avatar answered Oct 01 '22 01:10

Sven Marnach