1

I am working with numpy array as follows:

  input_series =  ['BUY' 'SELL' 'BUY' 'SELL' 'BUY' 'SELL' 'SELL' 'SELL' 'BUY' 'SELL' nan nan
     nan nan nan nan nan nan nan]

I am searching for particular values in array and if element exist then delete

I have done this as follows:

delete_indices = list()
val = ['BUY','SELL','No','YES']
found_index = np.where(lowercase_series_nparray == val)                                                
delete_indices.append(found_index)

delete_indices getting as follows:

[(array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([0, 2, 4, 8], dtype=int64),), (array([1, 3, 5, 6, 7, 9], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),)]

Aftre that I am trying to delete with :

new_output_series = numpy.delete(input_series, delete_indices)

But getting error as setting an array element with a sequence.

2 Answers 2

5

If from an array like this:

input_series = np.array(['BUY', 'a', 'b', 'SELL', 'YES', 'SELL', 'No', 'c', 'd', 'SELL'])

you want to remove these elements:

['BUY','SELL','No','YES']

Just set these as an array:

val = np.array(['BUY','SELL','No','YES'])

and then:

new_output_series = np.setdiff1d(input_series,val)

Output:

 ['a' 'b' 'c' 'd']
Sign up to request clarification or add additional context in comments.

Comments

2

Below statement gives you the indexes you need:

 found_index = np.in1d(input_series, val).nonzero()[0]

and then:

 new_array = numpy.delete(input_series, found_index)

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.