NumPy Tutorial NumPy Statistics NumPy References

NumPy - Array Manipulation



The NumPy has a number of built-in functions which can be used for manipulation of elements in ndarray object. Below is the list of most commonly used functions for array manipulation:

Changing array shape

FunctionDescription
ndarray.flat A 1-D iterator over the array.
reshape() Gives a new shape to an array without changing its data.
ravel() Returns a contiguous flattened array.
ndarray.flatten() Return a copy of the array collapsed into one dimension.

Transpose like operations

FunctionDescription
ndarray.T Returns the transposed array.
rollaxis() Roll the specified axis backwards, until it lies in a given position.
swapaxes() Interchange two axes of an array.
transpose() Reverses or permutes the axes of an array.

Joining Arrays

FunctionDescription
concatenate() Returns a concatenated array along the specified axis.
stack() Join a sequence of arrays along a new axis.
hstack() Stack arrays in sequence horizontally (column wise).
vstack() Stack arrays in sequence vertically (row wise).

Splitting Arrays

FunctionDescription
split() Split an array into multiple sub-arrays.
hsplit() Split an array into multiple sub-arrays horizontally (column-wise).
vsplit() Split an array into multiple sub-arrays vertically (row-wise).

Changing dimensions

FunctionDescription
broadcast() Produce an object that mimics broadcasting.
broadcast_to() Broadcast an array to a new shape.
expand_dims() Expand the shape of an array.
squeeze() Remove axes of length one from an array.

Adding and removing elements

FunctionDescription
append() Appends values to the end of an array.
resize() Return a new array with the specified shape.
insert() Insert values along the given axis before the given indices.
delete() Return a new array with sub-arrays along an axis deleted.
unique() Find the unique elements of an array.