Home > Software engineering >  Delete all subarrays having "True" for all elements from a given array
Delete all subarrays having "True" for all elements from a given array

Time:10-11

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:

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.

CodePudding user response:

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]]
  • Related