In this article, we will discuss how to create 1D or 2D numpy arrays filled with zeros (0s).
numpy.zeros()
Python’s Numpy module provides a function to create a numpy array of given shape & type and filled with 0’s i.e,
numpy.zeros(shape, dtype=float, order='C')
Arguments:
 shape: Shape of the numpy array. Single integer or sequence of integers.
 dtype: (Optional) Data type of elements. Default is float64.
 order: (Optional) Order in which data is stored in multidimension array i.e. in row major(‘F’) or column major (‘C’). Default is ‘C’.
Returns:
 It returns a numpy array of given shape but filled with zeros.
Let’s understand with some examples but first we need to import the numpy module,
import numpy as np
Create 1D Numpy Array of given length and filled with zeros
Suppose we want to create a numpy array of five zeros (0s). For that we need to call the numpy.zeros() function with argument 5 i.e.
np.zeros(5)
It returns a 1D numpy array with five 0s,
array([0., 0., 0., 0., 0.])
We can assign the array returned by zeros() to a variable and print its type to confirm if it is a numpy array or not,
arr = np.zeros(5) print(arr) print(type(arr))
Output:
[0. 0. 0. 0. 0.] <class 'numpy.ndarray'>
Create Numpy array of zeros of integer data type
By default numpy.zeros() returns a numpy array of float zeros. But if we want to create a numpy array of zeros as integers, then we can pass the data type too in the zeros() function. For example,
arr = np.zeros(5, dtype=np.int64) print(arr)
Output:
[0 0 0 0 0]
It returned a numpy array of zeros as integers because we pass the datatype as np.int64.
Create two dimensional (2D) Numpy Array of zeros
To create a multidimensional numpy array filled with zeros, we can pass a sequence of integers as the argument in zeros() function. For example, to create a 2D numpy array or matrix of 4 rows and 5 columns filled with zeros, pass (4, 5) as argument in the zeros function.
arr_2d = np.zeros( (4, 5) , dtype=np.int64) print(arr_2d)
Output:
[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]
It returned a matrix or 2D Numpy Array of 4 rows and 5 columns filled with 0s.
Create 3D Numpy Array filled with zeros
To create a 3D Numpy array filled with zeros, pass the dimensions as the argument in zeros() function. For example,
arr_3d = np.zeros( (2, 4, 5) , dtype=np.int64) print(arr_3d)
Output:
[[[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]]
It created a 3D Numpy array of shape (2, 4, 5).
Summary:
In this article we learned how to create 1D or 2D numpy array of given shape and filled with zeros.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.