Jamgreen
Jamgreen

Reputation: 11039

Delete from numpy arrays

I have two arrays in NumPy

a = np.array([])
b = np.array([])

These arrays are being populated throughout the code with cohesive values. But now I want to delete elements from both arrays where values in a are greater than the number 5.

I guess it's something like

a = a[~a>5]

but I don't know how to delete the element with exact same index in the array b.

Upvotes: 1

Views: 157

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You can use np.extract to select the specific elements and reassigned to your array again :

>>> x = np.arange(10)
>>> x=np.extract(x<5,x)
>>> x
array([0, 1, 2, 3, 4])

numpy.extract(condition, arr)

Return the elements of an array that satisfy some condition.

You can also use indexing for such task :

>>> x = np.array([3,4,7,11,0,34,6,1,3,4,2])
>>> x[x<5]
array([3, 4, 0, 1, 3, 4, 2])
>>> np.extract(x<5,x)
array([3, 4, 0, 1, 3, 4, 2])

Upvotes: 1

Related Questions