2011-09-06 72 views
9

我有一个Numpy Rec阵列,我想从中进行类似于SQL的一些快速查询:SELECT * where array['phase'] == "P"。我想获得一个记录数组作为输出,每行对应于原始数组中符合查询条件的一行。有任何想法吗?我很确定我以前做过这个,但是不记得这个功能。从Numpy Rec Array中选择行

由于

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')]) 

回答

7

尝试:

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

array['phase']=='P'返回一个布尔numpy的阵列。当idx是布尔数组时,array[idx]返回由这些行组成的数组,其中idxTrue

+1

这是天才!但是为了澄清其他ppl:这里的数组并不是指numpy.array,它指的是你的记录数组的名称。例如,如果我有一个名为data的recarray,那么我会想做:data [data ['phase'] =='P']] – Rishi

0

试试这个代码:

array[array.phase=='P']