Selecting rows from a NumPy ndarray

前端 未结 4 1096
[愿得一人]
[愿得一人] 2021-01-31 18:32

I want to select only certain rows from a NumPy array based on the value in the second column. For example, this test array has integers from 1 to 10 in the second column.

4条回答
  •  孤街浪徒
    2021-01-31 18:38

    This is two times faster than Amnon's variant for len(test)=1000:

    wanted = (2,4,6)
    wanted2 = numpy.expand_dims(wanted, 1)
    print test[numpy.any(test[:, 1] == wanted2, 0), :]
    

提交回复
热议问题