0

I have a very large numpy array that looks similar to my example. The partial string that I'm trying to detect is "F_H" and it's usually on column 0 of my array.

a = np.array([['#define', 'bad_stringF_H', 'some_value'],
              ['#define', 'good_string', 'some_value2'],
              ['#define', 'good_string_2', 'some_value3'],
              ['#define', 'bad_string2F_H', 'some_value4']])

I just want to delete the whole array if that partial string is detected in the row so the desired output would be like this.

[['#define' 'good_string' 'some_value2']
 ['#define' 'good_string_2' 'some_value3']]

1 Answer 1

3

You can use NumPy's Boolean indexing to create a new array that only includes the rows that do not contain the string 'F_H':

import numpy as np

a = np.array([['#define', 'bad_stringF_H', 'some_value'],
              ['#define', 'good_string', 'some_value2'],
              ['#define', 'good_string_2', 'some_value3'],
              ['#define', 'bad_string2F_H', 'some_value4']])

mask = np.array('F_H' not in x[1] for x in a])
print(mask)
new_a = a[mask]

print(new_a)
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe 'F_H' not in x[1] would be a tad bit more efficient.

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.