从Numpy记录数组中选择行
我有一个 Numpy 的记录数组,我想像在 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')])
2 个回答
0
试试这段代码:
array[array.phase=='P']
9
试试这个:
array[array['phase']=='P']
array['phase']=='P'
这个表达式会返回一个布尔类型的 numpy 数组。简单来说,当 idx
是一个布尔数组时,array[idx]
会返回那些在 idx
中值为 True
的行。