9

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

谢谢

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

2 回答 2

8

尝试:

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

array['phase']=='P'返回一个布尔 numpy 数组。当idx是一个布尔数组,array[idx]返回一个由那些行组成的数组 where idxis True

于 2011-09-06T00:22:21.753 回答
0

试试这个代码:

array[array.phase=='P'] 
于 2013-07-05T02:35:53.070 回答