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()
numpy.empty(shape, dtype=float, order='C')
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()
numpy.append(arr, values, axis=None)
It accepts following arguments,
Frequently Asked:
- Remove All Occurrences of a Value from NumPy Array
- Create a 1D / 2D Numpy Arrays of zeros or ones
- Create a CSV file from a list in Python
- Check if a NumPy Array contains a specific string
- 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,
# Create an empty Numpy array with 4 columns or 0 rows empty_array = np.empty((0, 4), int) print('Empty 2D Numpy array:') print(empty_array)
Output:
Empty 2D Numpy array: []
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,
# Append a row to the 2D numpy array empty_array = np.append(empty_array, np.array([[11, 21, 31, 41]]), axis=0) # Append 2nd rows to the 2D Numpy array empty_array = np.append(empty_array, np.array([[15, 25, 35, 45]]), axis=0) print('2D Numpy array:') print(empty_array)
Output:
2D Numpy array: [[11 21 31 41] [15 25 35 45]]
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,
# Append multiple rows i.e 2 rows to the 2D Numpy array empty_array = np.append(empty_array, np.array([[16, 26, 36, 46], [17, 27, 37, 47]]), axis=0) print('2D Numpy array:') print(empty_array)
Output:
2D Numpy array: [[11 21 31 41] [15 25 35 45] [16 26 36 46] [17 27 37 47]]
Create Empty Numpy array and append columns
Let’s create an empty Numpy array with 4 rows or 0 columns,
# Create an empty 2D numpy array with 4 rows and 0 column empty_array = np.empty((4, 0), int) print('Empty 2D Numpy array:') print(empty_array)
Output:
Empty 2D Numpy array: []
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
column_list_1 = [11, 21, 31, 41] # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, np.array([column_list_1]).transpose(), axis=1) print('2D Numpy array:') print(empty_array)
Output:
2D Numpy array: [[11] [21] [31] [41]]
So, one column is added to the empty numpy array. Now let’s add another column,
column_list_2 = [15, 25, 35, 45] # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, np.array([column_list_2]).transpose(), axis=1) print('2D Numpy array:') print(empty_array)
Output:
2D Numpy array: [[11 15] [21 25] [31 35] [41 45]]
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,
# Create an empty 2D numpy array with 4 rows and 0 column empty_array = np.empty((4, 0), int) column_list_2 = np.array([[16, 26, 36, 46], [17, 27, 37, 47]]) # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, column_list_2.transpose(), axis=1) print('2D Numpy array:') print(empty_array)
Output:
2D Numpy array: [[16 17] [26 27] [36 37] [46 47]]
The complete example is as follows,
import numpy as np def main(): print('*** numpy create empty array and append ***') print('*** Create Empty Numpy array and append rows ***') # Create an empty Numpy array with 4 columns or 0 rows empty_array = np.empty((0, 4), int) print('Empty 2D Numpy array:') print(empty_array) # Append a row to the 2D numpy array empty_array = np.append(empty_array, np.array([[11, 21, 31, 41]]), axis=0) # Append 2nd rows to the 2D Numpy array empty_array = np.append(empty_array, np.array([[15, 25, 35, 45]]), axis=0) print('2D Numpy array:') print(empty_array) # Append multiple rows i.e 2 rows to the 2D Numpy array empty_array = np.append(empty_array, np.array([[16, 26, 36, 46], [17, 27, 37, 47]]), axis=0) print('2D Numpy array:') print(empty_array) print('*** Create Empty Numpy array and append columns ***') # Create an empty 2D numpy array with 4 rows and 0 column empty_array = np.empty((4, 0), int) print('Empty 2D Numpy array:') print(empty_array) print('Append 1 column to the empty eD Numpy array') column_list_1 = [11, 21, 31, 41] # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, np.array([column_list_1]).transpose(), axis=1) print('2D Numpy array:') print(empty_array) print('Append 2nd column') column_list_2 = [15, 25, 35, 45] # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, np.array([column_list_2]).transpose(), axis=1) print('2D Numpy array:') print(empty_array) print('Append Multiple columns to the 2D Numpy Array') # Create an empty 2D numpy array with 4 rows and 0 column empty_array = np.empty((4, 0), int) column_list_2 = np.array([[16, 26, 36, 46], [17, 27, 37, 47]]) # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, column_list_2.transpose(), axis=1) print('2D Numpy array:') print(empty_array) if __name__ == '__main__': main()
Output:
*** numpy create empty array and append *** *** Create Empty Numpy array and append rows *** Empty 2D Numpy array: [] 2D Numpy array: [[11 21 31 41] [15 25 35 45]] 2D Numpy array: [[11 21 31 41] [15 25 35 45] [16 26 36 46] [17 27 37 47]] *** Create Empty Numpy array and append columns *** Empty 2D Numpy array: [] Append 1 column to the empty 2D Numpy array 2D Numpy array: [[11] [21] [31] [41]] Append 2nd column 2D Numpy array: [[11 15] [21 25] [31 35] [41 45]] Append Multiple columns to the 2D Numpy Array 2D Numpy array: [[16 17] [26 27] [36 37] [46 47]]