Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy select rows based on condition

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]]
like image 244
charelf Avatar asked Sep 24 '19 11:09

charelf


People also ask

How do I select a specific row in NumPy?

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.

How do you select values from an NP array?

To select an element from Numpy Array , we can use [] operator i.e. It will return the element at given index only.

How can we use conditions in NumPy within an array?

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]]) .


2 Answers

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, :]
like image 126
Mad Physicist Avatar answered Sep 21 '22 08:09

Mad Physicist


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]]
like image 43
ravishankar chavare Avatar answered Sep 20 '22 08:09

ravishankar chavare