numpy.append() in Python

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 the parameters are:

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:

The output should be:

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

Example 2:

The output should be:

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

Append 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]]

395

LEAVE A REPLY

Please enter your comment!
Please enter your name here