Create NumPy empty array and append in Python

In this post, we will learn how to Create NumPy empty array and append in Python. To create an empty NumPy array np. empty() function is used and so will understand this fist and will use in our program.

Numpy Empty() function

To create an empty numpy array the Python Numpy library empty() function is used. It creates an empty array of the given shape. The empty() function takes two arguments shapes and datatype.

Syntax

```numpy.empty(shape,datatype=float,order={c,f},like=none)
```

Parameters

• Shape :It is shape of empty array like (2,3)
• datatype: The datatype of output array by default it is numpy.float64()
• order :Whether to store multidimesional array data row-wise(c) or column-wise (f).
• like :array_like

Return

It returns a new array of given shapes and sizes. But without initializing entries.

1. Create NumPy empty array using empty() function

In this example, we are creating a Python Numpy empty array of shape 0 rows and 3 columns function. This is how to create NumPy empty array by using the numpy.empty() function.

```import numpy as np

#numpy 2d array initialize
nparr = np.empty((0, 3), int)

print(nparr)

#output : []
```

2. Create empty numpy 2d array and append

In this Python program, first, we will create an empty array of shape 0 rows and 3 columns.In the next step we will append row of same shape and pass axis=0.The numpy array append by row-wise with axis =0.

Python program to numpy append to 2D array

```import numpy as np

nparr_empty = np.empty((0, 3), int)

print('Create  2D Numpy Empty array:')
print(nparr_empty)

nparr_empty = np.append(nparr_empty, np.array([[3, 6, 9]]), axis=0)

nparr_empty = np.append(nparr_empty, np.array([[4, 35, 40]]), axis=0)

print('appended to 2D Numpy array:')
print(nparr_empty)

```

Output

```Create 2D Numpy Empty array:
[]
appended to 2D Numpy array:
[[ 3  6  9]
[ 4 35 40]]
```

3. Create empty NumPy 2D array and append mutiple rows

In this python program, we have created numpy empty array and append multiple rows of same shape and pass axis=0. To append multiple rows in this numpy empty array.Our 2D numpy array has shape of 0 row and 3 columns so need to pass same shape row while appending to avoid error.

Program to create 2D NumPy empty array and append mutiple rows

```import numpy as np

nparr_empty = np.empty((0, 3), int)

array_to_append = [[12,13,14],[6,12,18]]

result_arr = np.append(nparr_empty, array_to_append,axis=0 )

print(result_arr)

```

Output

```[[12 13 14]
[ 6 12 18]]
```

4. Create empty Numpy array and append :list.append()

To create an empty numpy array without shape, we have declared an empty list and user list.append() method to append elements and np.array() method to convert list to numpy array.

Python program to append NumPy array

```import numpy as np

array_to_append = [[10,10,14],[6,12,18],[15,16,14],[6,6,6]]

empty_nparr = []
empty_nparr.append(array_to_append)

np_arr = np.array(empty_nparr)

print(np_arr)

```

Output

```[[[10 10 14]
[ 6 12 18]
[15 16 14]
[ 6  6  6]]]
```

5. Create empty Numpy array append mutiple row list.append()

In this Python program we have appended multiple lists to an empty list and finally, convert them to a numpy array using the np. array() function.

Program to append to empty NumPy array

```import numpy as np

mylist = []

mylist.append([3,6,59])
mylist.append([12,7,15])

np_arr = np.array(mylist)

print(np_arr)

```

Output

```[[ 3  6 59]
[12  7 15]]
```

6. Create Numpy empty array and Append in loop

In this Python example we have created an empty numpy array of shape(0,4) and used for loop to append element to empty array.

Python Program to append to Numpy array using loop

```import numpy as np
nparr = np.empty((0, 4))
for i in np.arange(3):
append_val = np.random.rand(3, 4)
nparr = np.append(nparr, append_val, axis = 0)
print(nparr)
```

Output

```[[0.460475   0.3240884  0.98679217 0.22550795]
[0.67525692 0.46015025 0.54153917 0.80246006]
[0.90837824 0.5911019  0.70179784 0.5589747 ]]
```

Summary

In this post we have learned NumPy create empty array and append in Python with help of numpy.empty(),how to append in 2D numpy array a row or multiple rows,how to append NumPy array using list.append()