numpy.append#
- numpy.append(arr,values,axis=None)[source]#
Append values to the end of an array.
- Parameters:
- arrarray_like
Values are appended to a copy of this array.
- valuesarray_like
These values are appended to a copy ofarr. It must be of thecorrect shape (the same shape asarr, excludingaxis). Ifaxis is not specified,values can be any shape and will beflattened before use.
- axisint, optional
The axis along whichvalues are appended. Ifaxis is notgiven, botharr andvalues are flattened before use.
- Returns:
- appendndarray
A copy ofarr withvalues appended toaxis. Note that
appenddoes not occur in-place: a new array is allocated andfilled. Ifaxis is None,out is a flattened array.
Examples
>>>importnumpyasnp>>>np.append([1,2,3],[[4,5,6],[7,8,9]])array([1, 2, 3, ..., 7, 8, 9])
Whenaxis is specified,values must have the correct shape.
>>>np.append([[1,2,3],[4,5,6]],[[7,8,9]],axis=0)array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>>np.append([[1,2,3],[4,5,6]],[7,8,9],axis=0)Traceback (most recent call last):...ValueError:all the input arrays must have same number of dimensions, butthe array at index 0 has 2 dimension(s) and the array at index 1 has 1dimension(s)
>>>a=np.array([1,2],dtype=int)>>>c=np.append(a,[])>>>carray([1., 2.])>>>c.dtypefloat64
Default dtype for empty ndarrays is
float64thus making the output of dtypefloat64when appended with dtypeint64
On this page