How to remove array rows that contain only 0 using NumPy? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Numpy library provides a function called numpy.all() that returns True when all elements of n-d array passed to the first parameter are True else it returns False. Thus, to determine the entire row containing 0's can be removed by specifying axis=1. It will traverse each row and will check for the condition given in first parameter. Example: data=[[1,2,3] [0,0,0] [9,8,7]] After removing row with all zeroes: data=[[1,2,3] [9,8,7]] Example 1: Approach Followed: Take a numpy n-d array.Remove rows that contain only zeroes using numpy.all() function.Print the n-d array. Python3 import numpy as np # take data data = np.array([[1, 2, 3], [0, 0, 0], [4, 5, 6], [0, 0, 0], [7, 8, 9], [0, 0, 0]]) # print original data having rows with all zeroes print("Original Dataset") print(data) # remove rows having all zeroes data = data[~np.all(data == 0, axis=1)] # data after removing rows having all zeroes print("After removing rows") print(data) Output: Example 2: Approach Followed: Take 20 random numbers between 0-10, using numpy.random.choice() method.Align them in rows and columns, using reshape() method.Explicitly mark some rows as completely 0.Remove rows having all zeroes.Print dataset. Python3 import numpy as np # take random data # random.choice(x,y) will pick y elements from range (0,(x-1)) data = np.random.choice(10, 20) # specify the dimensions of data i.e (rows,columns) data = data.reshape(5, 4) # print original data having rows with all zeroes print("Original Dataset") print(data) # make some rows entirely zero data[1, :] = 0 # making 2nd row entirely 0 data[4, :] = 0 # making last row entirely 0 # after making 2nd and 5th row as 0 print("After making some rows as entirely 0") print(data) data = data[~np.all(data == 0, axis=1)] # data after removing rows having all zeroes print("After removing rows") print(data) Output: Comment More info R rohanchopra96 Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Dictionaries in Python 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like