AppDividend
Latest Code Tutorials

# Python numpy append() Function | Numpy.append()

Python numpy append() is a function that is used to merge two arrays. The numpy append() function returns a new array, and the original array remains unchanged. In Python numpy, sometimes, we need to merge two arrays. So for that, we have to use numpy.append() function. This function is used to append one array with another one, and then it returns the merged array.

## Python numpy append()

Python numpy.append(array, values, axis = None) method appends values along the mentioned axis at the end of the array.

### Syntax

`numpy.append(array, values, axis = None)`

The numpy.append() method takes three arguments, they are:

1. array: This can be an array or array object to which the append will be performed with some values.
2. values: This is the value, which is to be appended with the array.
3. Axis: This specifies the axis of the array along which the value is to be appended. There are mainly two types of axis:
• axis=1 -> indicates the axis of the first row
• axis=0 -> suggests the axis of the first column

### Return Value

The append() function returns a copy of the array with the values which are appended at the end as per the mentioned axis.

### Programming Example

#### Program1: Working with a 1D array

```# Importing numpy
import numpy as np

# We will create an 1D array

# this will create an array with values 0 to 5
arr1 = np.arange(6)
# Printing the array
print("The first array is: ", arr1)

# Creating a 2nd array
arr2 = np.arange(10, 16)
print("The second array is: ", arr2)

# Now we will append arr2 to arr1
# result will be copied to a new array
arr = np.append(arr1, arr2)

# Printing the new array
print("After append the new array is: ", arr)
```

#### Output

```The first array is:  [0 1 2 3 4 5]
The second array is:  [10 11 12 13 14 15]
After append the new array is:  [ 0  1  2  3  4  5 10 11 12 13 14 15]```

In this program, we can see that we have first made two arrays with arange() function, which creates an array with some specified value range. After that, we printed these two arrays, respectively.

Finally, we have called the append() function in which we have appended arr2 to arr1. One thing we can see here is, we have not specified the axis in this case, this is because the given arrays are 1D array. Axis is defined in a multidimensional array.

#### Program2: Working with a 2D array

```# Importing numpy
import numpy as np

# Creating a 2D array with shape 2x3
arr1 = np.array([(1, 2, 3), (4, 5, 6)])
print("The first array is:\n", arr1)

# Creating a second 2D array using arange function

# This will create a 2D array of shape 2x3 with values 10 to 15
arr2 = np.arange(10, 16).reshape(2, 3)
print("The second array is:\n", arr2)

# Now we will first append the array by axis 1
arr3 = np.append(arr1, arr2, axis=1)
print("After append by axis 1, new array is:\n", arr3)

# Now we will first append the array by axis 0
arr4 = np.append(arr1, arr2, axis=0)
print("After append by axis 0, new array is:\n", arr4)
```

#### Output

```The first array is:
[[1 2 3]
[4 5 6]]
The second array is:
[[10 11 12]
[13 14 15]]
After append by axis 1, new array is:
[[ 1  2  3 10 11 12]
[ 4  5  6 13 14 15]]
After append by axis 0, new array is:
[[ 1  2  3]
[ 4  5  6]
[10 11 12]
[13 14 15]]
```

In this program, we have created an array using numpy arange() and declared two arrays with shape 2×3 using two different ways. Then we called append() function to append arr2 to arr1 by axis 1, which means this will add row1 of arr1 with row1 of arr2 and so on.

Next, we have again called append() function to append arr2 to arr1 with axis=0, which means it will append column 1 of arr1 with column 1 of arr2 and so on.

Finally, we can see the output of these two operations.

## Python Numpy: Flattening Two Arrays

By “flattening” an array, we mean taking the multidimensional array and turning it into a regular “single” dimensional array.

See the following code.

```# app.py

import numpy as np

arrA = np.array([[11, 21], [30, 46]])
arrB = np.array([[19, 21], [46, 21]])

# no axis provided, array elements will be flattened
arr_flat = np.append(arrA, arrB)

print(arr_flat)
```

#### Output

```python3 app.py
[11 21 30 46 19 21 46 21]```

As we can see, we have not provided any axis; that is why it flattens the array.

## Python numpy: Merging Along Axis

We can merge two arrays along the axis using numpy append() function. See the following code.

```import numpy as np

arr_merge_axis0 = np.append([[11, 21], [29, 46]], [[19, 21], [37, 10]], axis=0)
print(f'Merged 2x2 Arrays along Axis-0:\n{arr_merge_axis0}')

arr_merge_axis1 = np.append([[11, 21], [29, 46]], [[19, 21], [37, 10]], axis=1)
print(f'Merged 2x2 Arrays along Axis-1:\n{arr_merge_axis1}')
```

#### Output

```python3 app.py
Merged 2x2 Arrays along Axis-0:
[[11 21]
[29 46]
[19 21]
[37 10]]
Merged 2x2 Arrays along Axis-1:
[[11 21 19 21]
[29 46 37 10]]```

From the output, we can derive the following statements.

1. When 2×2 arrays are merged along with the x-axis, the output array size is 4×2.
2. When 2×2 arrays are merged along with the y-axis, the output array size is 2×4.

## Python numpy: ValueError

Let’s look at an example where ValueError will be raised.

```import numpy as np

arr = np.append([[11, 21]], [[10, 19, 46]], axis=0)
print(arr)```

#### Output

```python3 app.py
Traceback (most recent call last):
File "app.py", line 3, in <module>
arr = np.append([[11, 21]], [[10, 19, 46]], axis=0)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/lib/function_base.py", line 4689, in append
return concatenate((arr, values), axis=axis)
ValueError: all the input array dimensions except for the concatenation axis must match exactly```

Array shapes are 1×2 and 2×3. So, the axis-1 shapes are different, that is why ValueError is raised.

## Conclusion

Numpy append() function adds values at the end of an input array. The append operation is not inplace, and a new array is allocated. One thing to note here that the dimensions of the input arrays must match; otherwise, ValueError will be raised.

This site uses Akismet to reduce spam. Learn how your comment data is processed.