Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete all subarrays having "True" for all elements from a given array

I have an array of arrays:

all_arrays = np.array([[True, True, True],
                       [True, True, False],
                       [True, True, True],
                       [False, False, False]])

and I need to remove all subarrays that contains "True" for all elements. In this case, it should remain only the 2nd one and the last one.

The result should look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array([[ True,  True, False],
       [False, False, False]])

I solved the problem by converting the array to a dataframe, perform the changes and then convert the dataframe back to an array

# Convert array to dataframe
zz = pd.DataFrame(all_arrays, columns = ['Column_A','Column_B','Column_C'])
# replace "True" with np.NaN
zz = zz.replace(True, np.nan)
# delete rows with all NaN
zz.dropna(how = 'all', inplace = True)
# replace back np.Nan with True
zz = zz.replace(np.nan, True)
# Convert dataframe array
all_arrays_filtered = zz.values

all_arrays_filtered

but I guess probably it is simpler and faster to do it using arrays but I don’t know how to do it

Thank you for any help.

>Solution :

Invert mask for test if all values per rows are Trues by numpy.all:

a = all_arrays[~all_arrays.all(axis=1)]
print (a)
[[ True  True False]
 [False False False]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading