numpy.append() in Python

The numpy.append() function in Python is used to add values to the end of the array and returns the new array. Dimensions of the input array must be matched otherwise ValueError will be generated.

Basic Syntax

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

And following are the parameters:

ParameterDescription
arrayspecify the input array
valuesThese values will be appended at the end of the array. The shape must be the same.
axisaxis=0 for column and axis = 1 for row. if not specified array will be flattened

Return value

This function will return the appended array with provided values through it.

Example

Following are examples for numpy.append() function.

Example 1: Following is an example for numpy.append() function

The output should be:

First Array : [0, 1, 2, 3]
Second Array : [8, 9])
Third array : [0, 1, 2, 3, 8, 9]

Example 2: Following is an another example for numpy.append() function

The output should be:

Initial items array:
[[0 1 2 3 4]
[5 6 7 8 9]]
Append all given elements to items array:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
Append all given elements to items array along axis 0:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
Append all given elements to items array along axis 1:
[[ 0 1 2 3 4 10 11 12 13 14]
[ 5 6 7 8 9 15 16 17 18 19]]

LEAVE A REPLY

Please enter your comment!
Please enter your name here