Skip to content
Advertisement

Finding matching subset of “row” in a numpy structured array

I have data stored in a NumPy structured array where part of the information identifies various cases. I would like to find the row that matches a given case. E.g., let’s say I’m storing the name of a building, room number, and the number of chairs and tables in the room in a (2,) array. This would then look something like this:

JavaScript

Now say that I want to find the row for building 'BLDG0', room 14. Based on the answer to Finding a matching row in a numpy matrix, I tried

JavaScript

which would ideally result in [2]. However, this results in the following warning:

JavaScript

and returns an empty array. Is there a way to find the matching sub-row for a large set of data other than comparing each column separately and then finding the matching indices?

I am using NumPy version 1.18.5 through miniconda and it doesn’t look like I can safely update to a newer version within this environment. (Though I’m not sure if newer versions support this type of comparison)

Advertisement

Answer

JavaScript

combine the two:

JavaScript

Use that as a boolean mask:

JavaScript

and getting the index:

JavaScript

Looks like we can test both fields, using a properly constructed structured array:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement