Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Rows from Numpy Rec Array

I have a Numpy rec array from which I would like to do some quick queries similar to SQL: SELECT * where array['phase'] == "P". I would like to get a Record Array as output with each row corresponding to a row from the original array that met the query criteria. Any ideas? I am pretty sure I have done this before, but just cannot remember the function.

Thanks

rec.array([ (5295499, 2.8123965, 127.20443, 0.0, 1237680436.06, 908, -19.942589, 134.33951, 0.3888, 'P', 0.19513991),
       (5295499, 2.8123965, 127.20443, 0.0, 1237680436.06, 1387, -18.102, 125.639, 0.11, 'P', 1.2515257),
       (5447254, 39.025873, 143.31065, 0.0, 1245455521.85, 1512, 33.121667, 130.87833, 0.573, 'LR', 45.099504)], 
      dtype=[('eventid', '<i4'), ('eventlat', '<f8'), ('eventlon', '<f8'), ('eventdepth', '<f8'), ('eventtime', '<f8'), ('stationid', '<i4'), ('stationlat', '<f8'), ('stationlon', '<f8'), ('stationelv', '<f8'), ('phase', '|S7'), ('timeresidual', '<f8')])
like image 238
Rishi Avatar asked Sep 06 '11 00:09

Rishi


People also ask

How do you access different rows of a multidimensional NumPy array?

In NumPy , it is very easy to access any rows of a multidimensional array. All we need to do is Slicing the array according to the given conditions. Whenever we need to perform analysis, slicing plays an important role.

How do I get rows and columns of a NumPy array?

In the NumPy with the help of shape() function, we can find the number of rows and columns. In this function, we pass a matrix and it will return row and column number of the matrix. Return: The number of rows and columns.

How do you select a row in a matrix in python?

We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.

How does NP select work?

select()() function return an array drawn from elements in choicelist, depending on conditions. Parameters : condlist : [list of bool ndarrays] It determine from which array in choicelist the output elements are taken.


1 Answers

Try:

array[array['phase']=='P']

array['phase']=='P' returns a boolean numpy array. When idx is a boolean array, array[idx] returns an array composed of those rows where idx is True.

like image 77
unutbu Avatar answered Sep 23 '22 14:09

unutbu