I'm trying to use numpy to remove rows from a two dimensional array where the first value of the row (so the element at index 0) does not match a certain condition.
I am able to do this with regular python using two loops, but I would like to do it more efficiently with numpy, e.g. with numpy.where
I have been trying various things with numpy.where
and numpy.delete
but I struggle with the fact that I want to select rows by using a condition that only needs to be verified by the first element, and not the second (I dont care about the value of the second element)
Here is an example where I only want to keep the rows where the first value of each row is 6.
Input:
[[0,4],
[0,5],
[3,5],
[6,8],
[9,1],
[6,1]]
Output:
[[6,8],
[6,1]]
We can use [][] operator to select an element from Numpy Array i.e. Example 1: Select the element at row index 1 and column index 2. Or we can pass the comma separated list of indices representing row index & column index too i.e.
To select an element from Numpy Array , we can use [] operator i.e. It will return the element at given index only.
It returns a new numpy array, after filtering based on a condition, which is a numpy-like array of boolean values. For example, if condition is array([[True, True, False]]) , and our array is a = ndarray([[1, 2, 3]]) , on applying a condition to array ( a[:, condition] ), we will get the array ndarray([[1 2]]) .
Use a boolean mask:
mask = (z[:, 0] == 6)
z[mask, :]
This is much more efficient than np.where
because you can use the boolean mask directly, without having the overhead of converting it to an array of indices first.
One liner:
z[z[:, 0] == 6, :]
Program:
import numpy as np
np_array = np.array([[0,4],[0,5],[3,5],[6,8],[9,1],[6,1]])
rows=np.where(np_array[:,0]==6)
print(np_array[rows])
Output:
[[6 8]
[6 1]]
And If You Want to Get Into 2d List use
np_array[rows].tolist()
Output of 2d List
[[6, 8], [6, 1]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With