Skip to content Skip to sidebar Skip to footer

Filter Numpy Structured Array Based On Multiple Values

I have a numpy structured array. : myArray = np.array([(1, 1, 1, u'Zone3', 9.223), (2, 1, 0, u'Zone2', 17.589), (3, 1, 1, u'Zone2', 26.95), (4, 0, 1, u'Zon

Solution 1:

You need to use np.in1d to test for membership of your criteriaList:

In [1]: myArray["ZoneName"] in criteriaList
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-ff2173ff4348> in <module>()
----> 1 myArray["ZoneName"] in criteriaList

ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()

In [2]: np.in1d(myArray["ZoneName"], criteriaList)
Out[2]: array([False,  True,  True,  True,  True], dtype=bool)

In [3]: myArray[(myArray["Flag1"] == 1) &
   ....:        (myArray["Flag2"] == 1) &
   ....:        np.in1d(myArray["ZoneName"], criteriaList)]["Value"].sum()
Out[3]: 31.344999999999999

Post a Comment for "Filter Numpy Structured Array Based On Multiple Values"