Python–Numpy 筛选出满足特定条件的行列

发布于 2022-08-31  895 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

筛选出第一个值为6的行

import numpy as np
np_array = np.array([[0,4],[0,6],[3,5],[6,8],[9,1],[6,1]])
rows=np.where(np_array[:,0]====6)
[out]:(array([3, 5], dtype=int64),)
print(np_array[rows])
[out]:
[[6 8]
 [6 1]]

筛选出第二个值为1的行

import numpy as np
np_array = np.array([[0,4],[0,6],[3,5],[6,8],[9,1],[6,1]])
rows=np.where(np_array[:,1]====1)
print(rows)
print(np_array[rows])
[out]:
(array([4, 5], dtype=int64),)
[[9 1]
 [6 1]]
Everything not saved will be lost.
最后更新于 2022-08-31