How to remove array rows that contain only 0 in python

Published: April 26, 2018

DMCA.com Protection Status

Let's consider a 2d matrix of dimension (20,6), called data:

[[3 5 1 8 6]
 [1 8 5 4 0]
 [0 6 8 2 0]
 [0 0 0 0 0]
 [9 2 3 6 0]
 [9 3 9 5 0]
 [4 9 7 6 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [1 0 2 0 7]
 [2 8 4 3 5]
 [9 8 8 4 3]
 [1 1 1 7 0]
 [8 9 4 9 9]
 [0 0 0 0 0]
 [5 8 6 7 5]
 [8 4 8 8 5]
 [0 0 0 0 0]
 [5 9 9 6 9]
 [6 9 9 5 9]]

Approach 1

To remove all rows that contain only 0 we can use the following syntax

data = data[~np.all(data == 0, axis=1)]

Example of code

import numpy as np

data = np.random.choice(10, 100)

data = data.reshape(20,5)

data[3,:] = 0
data[7,:] = 0
data[8,:] = 0
data[14,:] = 0
data[17,:] = 0

print(data)
print('original data shape', data.shape)

print('----- after removing rows with only 0 -----')

data = data[~np.all(data == 0, axis=1)]

print(data)
print('new data shape', data.shape)

that returns

----- after removing rows with only 0 -----
[[3 5 1 8 6]
[1 8 5 4 0]
 [0 6 8 2 0]
 [9 2 3 6 0]
 [9 3 9 5 0]
 [4 9 7 6 0]
 [1 0 2 0 7]
 [2 8 4 3 5]
 [9 8 8 4 3]
 [1 1 1 7 0]
 [8 9 4 9 9]
 [5 8 6 7 5]
 [8 4 8 8 5]
 [5 9 9 6 9]
 [6 9 9 5 9]]
new data shape (15, 5)

Approach 2

To remove all rows that contain only 0 we can also use the following syntax

 data= np.delete(data,np.where(~data.any(axis=1))[0], axis=0)

where

np.where(~data.any(axis=1))[0]

gives a list of rows with only 0 indexes.

One cab also use this approach to remove the columns that contain only 0, example:

import numpy as np

data = np.random.choice(10, 100)

data = data.reshape(20,5)

data[:,1] = 0
data[:,4] = 0


print(data)
print('original data shape', data.shape)

print('----- after removing rows with only 0 -----')

print(np.where(~data.any(axis=0))[0])


data= np.delete(data,np.where(~data.any(axis=0))[0], axis=1)

print(data)
print('new data shape', data.shape)

returns

[[7 0 8 8 0]
 [5 0 5 1 0]
 [6 0 2 4 0]
 [5 0 9 8 0]
 [6 0 3 9 0]
 [9 0 6 3 0]
 [3 0 4 6 0]
 [0 0 5 4 0]
 [3 0 1 4 0]
 [9 0 1 2 0]
 [3 0 4 2 0]
 [1 0 3 7 0]
 [0 0 0 3 0]
 [6 0 9 8 0]
 [0 0 2 1 0]
 [9 0 6 2 0]
 [0 0 3 6 0]
 [0 0 6 1 0]
 [6 0 8 0 0]
 [1 0 3 8 0]]
original data shape (20, 5)
----- after removing rows with only 0 -----
[1 4]
[[7 8 8]
 [5 5 1]
 [6 2 4]
 [5 9 8]
 [6 3 9]
 [9 6 3]
 [3 4 6]
 [0 5 4]
 [3 1 4]
 [9 1 2]
 [3 4 2]
 [1 3 7]
 [0 0 3]
 [6 9 8]
 [0 2 1]
 [9 6 2]
 [0 3 6]
 [0 6 1]
 [6 8 0]
 [1 3 8]]
new data shape (20, 3)

References