Home > OS >  How do I easily remove a string-element from a numpy array of strings?
How do I easily remove a string-element from a numpy array of strings?

Time:04-27

Say I have a numpy array of strings:

arr = np.array(['cat', 'dog', 'bird', 'swiss army knife'])

and I want to remove the string 'swiss army knife'.

What is the best way to do this?

It seems like something that should be very straight forward, but yet I haven't found a solution that doesn't involve sorting and/or finding the index of the element and use that to slice the selection of the array that's needed.

np.delete doesn't seem to work for strings.

CodePudding user response:

arr = np.array(['cat', 'dog', 'bird', 'swiss army knife'])
new_arr = np.array([i for i in arr if i != 'swiss army knife'])

CodePudding user response:

I hope this is works for you.

import numpy as np
arr = np.array(['cat', 'dog', 'bird', 'swiss army knife'])
result = np.where(arr=='swiss army knife')[0]
arr = np.delete(arr,result)
print(arr)

CodePudding user response:

just by masking:

mask = arr != 'swiss army knife'
result = arr[mask]
# ['cat' 'dog' 'bird']

or in one line as arr[arr != 'swiss army knife']

CodePudding user response:

arr = arr[arr != 'swiss army knife'] 

Numpy cheat sheet

http://datacamp-community-prod.s3.amazonaws.com/ba1fe95a-8b70-4d2f-95b0-bc954e9071b0

CodePudding user response:

There's no method to erase a value from a np.array based on the array's value. Numpy's delete erase value based on index. So, in this case, it would be like:

arr = np.array(['cat', 'dog', 'bird', 'swiss army knife'])
arr = np.delete(arr, 3)

Instead, you could just use:

arr = np.array(['cat', 'dog', 'bird', 'swiss army knife'])
arr = arr[arr != 'swiss army knife']

or

arr = np.array(['cat', 'dog', 'bird', 'swiss army knife'])
arr = np.delete(arr, np.argwhere(arr == 'swiss army knife'))
  • Related