Skip to content
LearnPY
NumPyfunction reference

ndarray.reshape

Return an array with the same data arranged in a new shape.

Signature

ndarray.reshape(shape, order='C')

What it does

reshape changes how the existing values are addressed without changing their number. The total number of elements must stay the same. A dimension of -1 asks NumPy to infer that length.

Parameters

shapeint or tuple of ints
The target shape. One dimension may be -1.
orderstr, optionaloptional
Read order: 'C' for row-major or 'F' for column-major.

Returns

ndarray

Example

pythonndarray.reshape example
import numpy as np

values = np.arange(6)
print(values.reshape(2, 3))

Output

[[0 1 2]
 [3 4 5]]

Watch out for

  • The element count must match; 6 values can become (2, 3) or (3, 2), not (4, 2).
  • Reshape returns a view when possible; mutating that view can affect the original array.