In this post, we are going to learn about how to add|append elements to NumPy array in Python. by using append(), insert(), concatenate() function with help of python program examples3 Methods to Add elements to Numpy array in Python
- Numpy.append()
- Numpy.insert()
- Numpy.concatenate()
1. add| append element to Numpy array using append()
The Numpy appends() function adds an element in a NumPy array at the end. This method does not modify the original array else returns a copy of the array after adding the passed element or array.
Syntax
numpy.append(arr,values,axis=none)
Parameters
- arr: The copy of the array in which values are to be appended.
- values: The values adding or append to array.The shape(size) must be same as arr exclude axis.If axis is not passed the value can be any shape and flatten before use.
- axis: The axis represent adding values row-wise or columns-wise.if none both array or values are flatten before use.
Python Program to add element to NumPy array Using append()
import numpy as np
myarr = np.array([12,14,700,60,50])
numarr = np.array([3,6,9])
#append an element at end of array
resarr = np.append(myarr, 90)
print('append an element in Numpy array: ', resarr)
#append array at end of array
resarr = np.append(myarr,numarr)
print('\n appended an Array in Numpy array: ', resarr)
Output
append an element in Numpy array: [ 12 14 700 60 50 90]
appended an Array in Numpy array : [ 12 14 700 60 50 3 6 9]
2. add| append element to Numpy array using insert()
The numpy library insert() function adds values in the numpy array along with the axis before the given index.
Syntax
numpy.insert(arr,obj,values,axis=none)
Parameters
- arr: The copy of the array in which values are to be appended.
- obj :objects define index or indices before values inserted.
- values :The values adding or append to array.The shape(size) must be same as arr exclude axis.If axis is not passed the value can be any shape and flatten before use.
- axis: The axis represent adding values row-wise or columns-wise.if none both array or values are flatten before use.
Python Program to add element to NumPy array Using append()
import numpy as np
arr = np.array([[10,12],[23,24],[15,16]])
print ('The original array is flattened before insert.when axis is not passed')
result_arr = np.insert(arr,2,[30,33])
print(result_arr)
print ('\n The axis 0: is passed:')
print (np.insert(arr,1,[45],axis = 0))
print ('\n The axis 1: is passed:\n')
print (np.insert(arr,1,56,axis = 1))
Output
The original array is flattened before insert.when axis is not passed
[10 12 30 33 23 24 15 16]
The axis 0: is passed:
[[10 12]
[45 45]
[23 24]
[15 16]]
The axis 1: is passed:
[[10 56 12]
[23 56 24]
[15 56 16]]
3.add|append element to Numpy array using concatenate()
The NumPy array concatenate() function concate two numpy array of same shape either row-wise or column-wise.This function by default concatenate arrays row-wise (axis=0).
Syntax
numpy.concatenate((a1, a2, ...an), axis=0, out=None, dtype=None)
Parameters
- a1..an : The sequence of array_like object.The sequence must be of same shape
- axis :The axis along which array is joined.It is optional parameter.
- axis=none: The input array is flatten before use.
- axis =1: The array is join column-wise
- axis = 0 : The array is joined row-wise.It is defulat value of axis.
- Out : optional The output ndarray if provided output is placed in this array.The shape must be correct.
- dtype : optional if provided the output array of this type or str.
Python program to Add|append to Numpy array using concatenate()
import numpy as np
nparr1 = np.array([13, 14, 15, 18, 20])
nparr2 = np.array([22, 32, 33, 34, 36])
joined_nparr = np.concatenate( (nparr1, nparr2) )
print(joined_nparr)
Output
[13 14 15 18 20 22 32 33 34 36]
Python program to add|append 2D Numpy array using concatenate()
import numpy as np
nparr1 = np.array([[13, 14, 15],
[26, 12, 23],
[24, 25, 30]])
nparr2 = np.array([ [11, 54, 9],
[3, 6, 7],
[4, 8, 62]])
# Concatenate 2D np Arrays column wise
joined_nparr = np.concatenate( (nparr1, nparr2), axis=1 )
# Concatenate 2D np Arrays row-wise
join_nparr_rowwise = np.concatenate( (nparr1, nparr2), axis=0 )
print('concatenate column-wise:\n',joined_nparr)
print('\n concatenate row-wise:',join_nparr_rowwise)
Output
concatenate column-wise:
[[13 14 15 11 54 9]
[26 12 23 3 6 7]
[24 25 30 4 8 62]]
concatenate row-wise: [[13 14 15]
[26 12 23]
[24 25 30]
[11 54 9]
[ 3 6 7]
[ 4 8 62]]
Summary
In this post, we have learned how to Add|Append elements to the Numpy array in Python with examples.