Remove rows of a numpy array based on a specific condition

numpy delete
np.delete multiple rows
np.delete not working
numpy select rows by condition
numpy array remove element by value
numpy where
delete rows array numpy
'numpy.ndarray' object has no attribute 'delete'

I have an array of four rows A = array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]]). In each row there are 4 numbers. How do I remove row#3 and row#4? In row#3 and row#4, 1 and 2 appear more than once respectively.

Is there a faster way to do it for arbitrary number of rows and columns? The main aim is to remove those rows where a non negative number appear more than once.

You can use something like this: first create dictionary of occurrences of each value in the sub arrays using np.unique and only keep arrays where no positive number appears more than once.

A = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])

new_array = []

# loop through each array
for array in A:
    # Get a dictionary of the counts of each value
    unique, counts = np.unique(array, return_counts=True)
    counts = dict(zip(unique, counts))
    # Find the number of occurences of postive numbers
    positive_occurences = [value for key, value in counts.items() if key > 0]
    # Append to new_array if no positive number appears more than once
    if any(y > 1 for y in positive_occurences):
        continue
    else:
        new_array.append(array)

new_array = np.array(new_array)

this returns:

array([[-1, -1, -1, -1],
       [-1, -1,  1,  2]])

NumPy: Extract or delete elements, rows and columns that satisfy , where() . In np. delete() , set the target ndarray , the index to delete and the target axis. In the case of a two-dimensional array, rows are deleted if axis=0 and columns are deleted if axis=1 . How to delete specific rows from a numpy array using a condition? Making statements based on opinion; back them up with references or personal experience

numpy.delete(): Delete rows and columns, Using the NumPy function np.delete(), you can delete any row and column from the NumPy array ndarray.numpy.delete — NumPy v1.15 Manual arrays. If you want to delete elements, rows and columns according to the condition With axis=None , the element at the index specified by obj is deleted after� Extract rows and columns that satisfy the conditions In the example of extracting elements, a one-dimensional array is returned, but if you use np.all () and np.any (), you can extract rows and columns while keeping the original ndarray dimension. All elements satisfy the condition: numpy.all ()

I modified also to store the indices:

A = np.array([[-1, -1, -1, -1], [-1, -1, 1, 2], [-1, -1, 1, 1], [2, 1, -1, 2]])

new_array = []
**indiceStore = np.array([])**

# loop through each array
for array in A:
    # Get a dictionary of the counts of each value
    unique, counts = np.unique(array, return_counts=True)
    counts = dict(zip(unique, counts))
    # Find the number of occurences of postive numbers
    positive_occurences = [value for key, value in counts.items() if key > 0]
    # Append to new_array if no positive number appears more than once
    if any(y > 1 for y in positive_occurences):
        **indiceStore = np.append(indiceStore, int(array))**
        continue
    else:
        new_array.append(array)

new_array = np.array(new_array)

Let me kniow if this is right.

Delete elements from a Numpy Array by value or conditions in Python, Remove all occurrences of an element with given value from numpy array Delete elements in Numpy Array based on multiple conditions. To delete a row from a 2D numpy array using np.delete () we need to pass the axis=0 along with numpy array and index of row i.e. row number, # Delete row at index 0 i.e. first row arr2D = np.delete(arr2D, 0, axis=0) print('Modified 2D Numpy Array by removing rows at index 0')

Delete elements, rows or columns from a Numpy Array by index , Now let's see how to delete rows and columns from it based on index positions. print('Modified 2D Numpy Array by removing columns at index 1') Delete specific elements in 2D Numpy Array by index position zeros or ones � Python Numpy : Select elements or indices by conditions from Numpy Array� 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

How to extract the entire row and columns when condition met in , remove rows from numpy array based on condition Home � Python � Selecting specific rows and columns from NumPy array Selecting specific rows and� Delete rows and columns; Example for multidimensional arrays; If you want to delete elements, rows and columns according to the condition instead of specifying the position, refer to the following post. Related: NumPy: Extract or delete elements, rows and columns that satisfy the conditions; Basic usage of np.delete() np.delete() takes three parameters as follows. numpy.delete(arr, obj, axis=None) arr: Input array; obj: Row or column number to delete

How to delete rows in a NumPy array in Python, Call numpy.delete(arr, obj, axis=None) with obj as a tuple of row indices to remove them from arr along a specified axis . an_array = np.array([[1, 2, 3], [4, 5, 6], [7,� Upvoting because this is the only answer which actually does what the OP wanted - to remove data from their array. Yes its nice to play with the strides/indexes to obtain views with just the data you want - but sometimes you just need to delete a row. – J.J May 8 '15 at 16:21

Comments
  • is there a way to know the indices of the rows which are not considered?
  • i modifed a bit. Please see my answer and let me know what i did is ok.