You are reading the article Working Of The Numpy Append() Function updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Working Of The Numpy Append() Function
Definition of NumPy appendThe NumPy append() function is used to append the values at the end of an array. The NumPy append() function is a built-in function in NumPy package of python. This function can be used to append the two array or append value or values at the end of an array, it adds or append a second array to the first array and return as a new array. This function does not change to the first array, it just returns the appended array, the first array will be the same as before calling the append() function.
Start Your Free Software Development Course
Syntax:
NumPy.append( array, values, axis = None)Parameters:
array – This is not an optional parameter, which specifies the array whose copy is to be appended with specified values. The array can be an n-dimensional array.
values – This is not an optional parameter, which specifies the values which are to be appended to the copy of the specified array. The shape of the values must match with the shape of the array, excluding the axis in appending. Values of any shape will be allowed if the axis is not defined which will be flattened before use.
axis – This is an optional parameter, which specifies the axis along which values will be appended. If the axis is not provided, then the array and value will be flattened before use.
Return value – The return value of this function is the NumPy array which is the copy of the array with the appended passed values to the axis.
Working of the NumPy append() functionThe NumPy append() function accepts three parameters (array, values, axis) and the first two parameters are mandatory parameters. If we pass the array “[11, 22, 33, 44]” as array and the array “[10, 20, 30, 40]” as values to the append() function, then append() function return the resultant array as “[11, 22, 33, 44, 10, 20, 30, 40]”. If we check the array, then it will be the same as before calling the append() function as “[11, 22, 33, 44]”.
Examples for the NumPy append () functionExample of NumPy append() function for appending 1-D array with values and array –
Next, we write the python code to understand the NumPy random append() function more clearly with the following example, where the append() function is used to appending a 1-D array with some values and array, as below –
Example #1Code:
# import numpy package as np import numpy as np # creating numbers of array array = np.array( [ 10, 20, 30, 40, 50 ] ) value = 100 app_array = np.append( array, value ) # printing array print("The array is : ", array ) # printing value print("The value is : ", value ) print( "The output of append(array, value) function is : " ) # printing the appended array print( app_array) # printing array to verify whether it is update or not print("The array is : ", array ) print("n") array2 = np.array( [ 11, 22, 33, 44, 55 ] ) app_array = np.append( array, array2 ) print("The array1 is : ", array ) # printing array2 print("The array2 is : ", array2 ) print( "The output of append(array1, array2) function is : " ) # printing the appended array print( app_array) # printing array to verify whether it is update or not print("The array is : ", array )Output:
As in the above program, the append() function is used to append array with value and array with array. The appended array we can see in the output, we can see how the value and array are appended, and also, we can see that the original array unchanged.
Next, we write the python code to understand the NumPy random append() function, where the append() function is used to appending a 2-D array with some values and array, as below –
Example #2Code:
# import numpy package as np import numpy as np # creating 2d array array = np.array([ [11, 22, 33], [44, 55, 66], [77, 88, 99] ]) value = 100 app_array = np.append( array, value ) print("The array is : ", array ) print("The value is : ", value ) print( "The output of append(2darray, value) function is : " ) # printing the appended array print(app_array) print("n") array2 = np.array( [ 11, 22, 33, 44, 55 ] ) app_array = np.append( array, array2 ) print("The array1 is : ", array ) # printing array2 print("The array2 is : ", array2 ) print( "The output of append(2darray1, 1darray2) function is : " ) # printing the appended array print(app_array) print("n") array3 = np.array([ [10], [20], [30] ]) app_array = np.append( array, array3 ) print("The array1 is : ", array ) print("The array3 is : ", array3 ) print( "The output of append(2darray1, 2darray2) function is : " ) print( app_array)Output:
As in the above program, the append() function is used to append 2-Darray with value, a 1-D array with a 1-D horizontal array, and 1-D array with 1-D vertical array without the axis. The appended arrays we can see in the output, we can see how the value and 1-D arrays are appended, but if we pass the axis value either 0 or 1, then append() function gives an error “ValueError: all the input arrays must have a same number of dimensions..”.
Next, we write the python code to understand the NumPy random append() function, where the append() function is used to appending 2-D array with some values and array along with axis value, as below –
Example #3Code:
# import numpy package as np import numpy as np # creating 2d array array1 = np.array([ [11, 22, 33], [44, 55, 66], [77, 88, 99] ]) array2 = np.array([ [10, 20, 30], [40, 50, 60], [70, 80, 90] ]) app_array1 = np.append( array1, array2, axis = 0 ) print("The array1 is : ", array1 ) print("The array3 is : ", array2 ) print( "The output of append(2darray1, 2darray2, axis=0) function is : " ) print( app_array1) print( "n" ) app_array2 = np.append( array1, array2, axis = 1 ) print( "The output of append(2darray1, 2darray2, axis=1) function is : " ) print( app_array2)Output:
As in the above program, the append() function is used to append 2-Darray with 2-D array(vertically and horizontally). The appended arrays we can see in the output, the first append is done vertically as axis value is 0 and the second append is done horizontally as axis value is 1.
ConclusionThe NumPy append() function is a built-in function in the NumPy package, which is used to append the values at the end of an array.
Recommended ArticlesThis is a guide to NumPy append. Here we also discuss the introduction, syntax, parameters, and different examples and its code implementation. You may also have a look at the following articles to learn more –
You're reading Working Of The Numpy Append() Function
Update the detailed information about Working Of The Numpy Append() Function on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!