{"id":8744,"date":"2023-11-04T19:15:51","date_gmt":"2023-11-04T13:45:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=8744"},"modified":"2023-11-10T12:17:28","modified_gmt":"2023-11-10T06:47:28","slug":"append-add-an-element-to-numpy-array-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/append-add-an-element-to-numpy-array-in-python\/","title":{"rendered":"Append\/ Add an element to Numpy Array in Python (3 Ways)"},"content":{"rendered":"
In this article we are going to discuss 3 different ways to add element to the numpy array.<\/p>\n
Let’s see one by one method.<\/p>\n
In numpy module of python there is a function numpy.append() <\/span><\/span>which can be used to add an element. We need to pass the element as the argument of the function.<\/p>\n Let\u2019s take a \u00a0example where an array is declared first and then we used the We can also insert a column by the use of \u00a0append() method .<\/p>\n Let\u2019s take an example below where we created a 2-Darray and we have to \u00a0insert two columns at a specific place.<\/p>\n We can also insert a row\u00a0 by the use of \u00a0append() method.<\/p>\n Let\u2019s take the \u00a0example below where we created a 2-D array and we have to \u00a0inserte \u00a0a row to it.<\/p>\n In numpy module of python there is a function Adding another array, see the below program<\/p>\n In numpy module of python there is a function Below program is to add an elements to the array.<\/p>\n Adding an element to Numpy Array in Python In this article we are going to discuss 3 different ways to add element to the numpy array. Let’s see one by one method. By using append() method By using concatenate() method By using insert() method Method-1 : By using append() method : In numpy module of …<\/p>\nappend()<\/code> method to add more elements \u00a0to the array.<\/p>\n
import numpy as np\r\narr = np.array([12, 24, 36])\r\nnew_Arr = np.append (arr, [48,50,64])\r\nprint(\u2018 array : \u2019,arr)\r\nprint(\u2018result = : \u2019 ,new_Arr)\r\n<\/pre>\n
Output :\r\narray : [ 12 \u00a024 \u00a036 ]\r\nresult = : [ 12 \u00a0\u00a024 \u00a0\u00a036 \u00a048\u00a0\u00a0 50\u00a0\u00a0 64 ]<\/pre>\n
import numpy as np\r\narr1= np.array([[12, 24, 36], [48, 50, 64]])\r\narr2 = np.array([[7], [8]])\r\nnew_Array = np.append(arr1,arr2, axis = 1)\r\nprint(' array : ',arr1)\r\nprint(' result = : ', new_Array)\r\n<\/pre>\n
Output :\r\narray : [ [ 12\u00a0 24\u00a0 36\u00a0 ]\u00a0 \r\n[ 48\u00a0 50\u00a0 64\u00a0 ] ]\r\nresult \u00a0: [\u00a0 [ 12 \u00a024\u00a0 36\u00a0 7\u00a0 ] \r\n[48\u00a0 50\u00a0 64\u00a0 \u00a08\u00a0 ]\u00a0 ]<\/pre>\n
import numpy as np \r\narr= np.array([[12, 24, 36], [48, 50, 62]])\r\nnew_Array = np.append(arr, [[70 ,80 ,90 ]], axis = 0)\r\nprint('array = ', arr )\r\nprint('result = ' ,new_Array)\r\n<\/pre>\n
Output :\r\narray = [ [ 12 \u00a024 \u00a0\u00a036 \u00a0\u00a0]\u00a0 \r\n[ 48 \u00a050\u00a0 \u00a0\u00a064 \u00a0\u00a0] ]\r\nresult = [\u00a0 [ 12 \u00a0\u00a024 \u00a0\u00a036 \u00a0\u00a0] \r\n[ 48 \u00a0\u00a050\u00a0 64 \u00a0\u00a0]\r\n[70\u00a0\u00a0 80 \u00a0\u00a090 ] ]<\/pre>\n
<\/a>Method-2 : By using concatenate() method :<\/h3>\n
numpy.concatenate()<\/code> to join two or more arrays. To add a single element we need to encapsulate the single value in a sequence data structure like list pass it to the function.<\/p>\n
import numpy as np\r\n# Numpy Array of integers created\r\narr = np.array([1, 2, 6, 8, 7])\r\n# Adding an element at the end of a numpy array\r\nnew_arr = np.concatenate( (arr, [20] ) )\r\nprint('array: ', new_arr)\r\nprint('result: ', arr)<\/pre>\n
Output :\r\narray:[1, 2, 6, 8, 7]\r\nresult: [1, 2, 6, 8, 7,20]<\/pre>\n
import numpy as np \r\narr1 = np.array([[10, 20], [30, 40]])\r\narr2 = np.array([[50, 60]])\r\nnew_Array= np.concatenate((arr1,arr2 ), axis=0)\r\nprint( 'First Array : ' ,arr1 )\r\nprint( 'Second Array : ' , arr2 )\r\nprint( 'concatenated Array : ' , new_Array )\r\n<\/pre>\n
Output :\r\nFirst Array :\u00a0\u00a0 [[ 10\u00a0 20 ]\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0[30\u00a0 40 ]]\r\nSecond Array :\u00a0 [[ 50\u00a0 60 ] ]\r\n Concatenated Array :\u00a0\u00a0\u00a0\u00a0 [[ 10\u00a0\u00a0 20 ]\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 [ 30\u00a0\u00a0 40 ]\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 [ 50\u00a0\u00a0\u00a0 60 ]]<\/pre>\n
<\/a>Method-3 : By using insert() method :<\/h3>\n
numpy.insert()<\/code> to add an element at the end of the numpy array.<\/p>\n
import numpy as np \r\narr= np.array ( [ 16, 33, 47, 59, 63 ,79 ])\r\n# Here specified at index 1, so elemnt will eb replaced with new element\r\nnew_Array = np.insert(arr, 1, 20 )\r\nprint('The array : ', arr)\r\nprint ('result :', new_Array)\r\n<\/pre>\n
Output :\r\narray : [ 16 \u00a033\u00a0 47\u00a0 59\u00a0 63\u00a0 79 ]\r\nresult : [ 16 \u00a020 \u00a033 \u00a047\u00a0 59\u00a0 66\u00a0 79 ]<\/pre>\n","protected":false},"excerpt":{"rendered":"