Given a 2xn array how do I slice the columns into a new array given a condition on the first row?
x = np.array([[1,2,3],[10,20,30]])
In:
Out[20]:
array([[ 1, 2, 3],
[10, 20, 30]])
Say I want all the column where row 0 is <= 2.
In: x[x[:]<=2]
Out: array([1, 2])
This didn't get me the 10 and 20 along with the 1 and 2. I'm expecting
array([[ 1, 2],
[10, 20]])
What is the right condition to put inside x[] to get my slice?
You can boolean slice the 2nd axis using:
x[:,x[0]<=2]
# returns:
array([[ 1, 2],
[10, 20]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.