1

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.

1
  • 1
    you can set an index ix = a <= 5 and then a = a[ix] and b = b[ix] (or do it in one line a = a[a <= 5] Commented May 8, 2015 at 7:38

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.