In this article we will discuss how to create an empty matrix or 2D numpy array first using numpy.empty() and then append individual rows or columns to this matrix using numpy.append().

Before moving forward, let’s have a quick look at the two functions which we are going to use in this article,

numpy.empty()

It accepts shape and data type as arguments. Then returns a new array of given shape and data type but without initializing entries.

numpy.append()

It accepts following arguments,

  • arr: copy of array in which value needs to be appended
  • values: array which needs to be appended on any axis, It must be of same shape as arr.
  • axis: Axis along which values need to be appended. To append as row axis is 0, whereas to append as column it is 1.

It creates a copy of the given numpy array arr and then appends the numpy array values to it along the given axis. Then it returns this new array i.e. copy of arr but with appended values.

Let’s use these two functions to create an empty 2D Numpy array and append items to it as rows or columns,

Create Empty Numpy array and append rows

Let’s create an empty Numpy array with 4 columns or 0 rows,

Output:

Now to append a new row to this empty 2D Numpy array, we can use the numpy.append(). But we need to pass the row as a numpy array of same shape only, and pass axis=0, so that it can be appended along the column,

Output:

Because our 2D Numpy array had 4 columns, therefore to add a new row we need to pass this row as a separate 2D numpy array with dimension (1,4) i.e. 1 row and 4 columns. It is important that we pass the row to be appended as the same shape of numpy array otherwise we can get following error,
ValueError: all the input arrays must have same number of dimensions,

Add multiple rows to an empty 2D Numpy array

To add multiple rows to an 2D Numpy array, combine the rows in a same shape numpy array and then append it,

Output:

Create Empty Numpy array and append columns

Let’s create an empty Numpy array with 4 rows or 0 columns,

Output:

Now to append a new column to this empty 2D Numpy array, we can use the numpy.append(). But we need to pass the column as a numpy array of same shape only and argument axis=1, so that it can be appended along the column

Output:

So, one column is added to the empty numpy array. Now let’s add another column,

Output:

Here we appended 2 columns to an empty 2D Numpy array.

Because our empty numpy array has 4 rows & 0 column, so to add a new column we need to pass this column as a separate 2D numpy array with dimension (4,1) i.e. 4 rows and 1 column.
It is important that we pass the column to be appended as the same shape of numpy array otherwise we can get following error,
ValueError: all the input arrays must have same number of dimensions,

Add multiple columns to an empty 2D Numpy array in single line

To add multiple columns to an 2D Numpy array, combine the columns in a same shape numpy array and then append it,

Output:

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.