There are two options: np.moveaxis and np.transpose.

  • np.moveaxis(a, sources, destinations) docs

    This function can be used to rearrange specific dimensions of an array. For example, to move the 4th dimension to be the 1st and the 2nd dimension to be the last:

    >>> rearranged_arr = np.moveaxis(arr, [3, 1], [0, 3])
    >>> rearranged_arr.shape
    (40, 10, 30, 20)
    

    This can be particularly useful if you have many dimensions and only want to rearrange a small number of them. e.g.

    >>> another_arr = np.random.rand(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    >>> np.moveaxis(another_arr, [8, 9], [0, 1]).shape
    (8, 9, 0, 1, 2, 3, 4, 5, 6, 7)
    
  • np.transpose(a, axes=None) docs

    This function can be used to rearrange all dimensions of an array at once. For example, to solve your particular case:

    >>> rearranged_arr = np.transpose(arr, axes=[3, 0, 2, 1])
    >>> rearranged_arr.shape
    (40, 10, 30, 20)
    

    or equivalently

    >>> rearranged_arr = arr.transpose(3, 0, 2, 1)
    >>> rearranged_arr.shape
    (40, 10, 30, 20)
    
Answer from Moormanly on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.moveaxis.html
numpy.moveaxis — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> x = np.zeros((3, 4, 5)) >>> np.moveaxis(x, 0, -1).shape (4, 5, 3) >>> np.moveaxis(x, -1, 0).shape (5, 3, 4)
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-moveaxis-with-examples-d8a2e0af5831
Understanding numpy.moveaxis with Examples | by Hey Amit | Medium
March 6, 2025 - “Mistakes are proof that you are trying.” — But let’s avoid them by understanding numpy.moveaxis better. Here are some common questions you might have! 1. What happens if source and destination have different lengths? This might surprise you: NumPy doesn’t like mismatches! If your source and destination lists have different lengths, NumPy will throw an error. Each axis you're trying to move must have a corresponding position to go to. ... import numpy as np arr = np.ones((2, 3, 4)) # Mismatched lengths: source has 2 axes, destination has 1 moved_arr = np.moveaxis(arr, (0, 1), (2,))
🌐
w3resource
w3resource.com › numpy › manipulation › moveaxis.php
NumPy: numpy.moveaxis() function - w3resource
April 23, 2026 - NumPy Array manipulation: numpy.moveaxis() function, example - The moveaxis() function is used to move axes of an array to new positions.
🌐
Stack Exchange
datascience.stackexchange.com › questions › 129866 › difficulty-understanding-moveaxis-in-numpy
python - Difficulty understanding .moveaxis in Numpy - Data Science Stack Exchange
August 4, 2024 - You asked for help on understanding the mechanism behind .moveaxis. The documentation explains it twice, imprecisely in English and very precisely in code when you click on [source]. There's not a lot going on there. We just pick a new ordering of the existing axes, specified by the caller. order = [n for n in range(a.ndim) if n not in source] for dest, src in sorted(zip(destination, source)): order.insert(dest, src) And then the final assignment essentially computes np.transpose(order), so that's the method you want to study in order to better understand this function.
🌐
GitHub
github.com › numba › numba › issues › 7369
Add np.moveaxis support. · Issue #7369 · numba/numba
September 5, 2021 - @overload(np.moveaxis) def moveaxis(a:np.ndarray, source, destination) -> np.ndarray: """ Move axes of an array to new positions. Other axes remain in their original order. Parameters ---------- a : np.ndarray The array whose axes should be ...
Author: numba
Find elsewhere
🌐
MindSpore
mindspore.cn › mindspore.numpy.moveaxis
mindspore.numpy.moveaxis | MindSpore 1.7 documentation | MindSpore
>>> import mindspore.numpy as np >>> x = np.zeros((3, 4, 5)) >>> output = np.moveaxis(x, 0, -1) >>> print(output.shape) (4, 5, 3) >>> output = np.moveaxis(x, -1, 0) >>> print(output.shape) (5, 3, 4) >>> output = np.moveaxis(x, [0, 1, 2], [-1, -2, -3]) >>> print(output.shape) (5, 4, 3)
🌐
Python.org
discuss.python.org › python help
Difficulty understanding .moveaxis in Numpy - Python Help - Discussions on Python.org
August 4, 2024 - I am learning Numpy in Python. I am having difficulties understanding .moveaxis in Numpy. I first create an array by using a=np.arange(24).reshape(2,3,4). The system will first fill up axis 2 with 0 1 2 3, then move along axis 1 to the next row. When the first ‘page’ is done, the system move along axis 0. The following is obtained. array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) If ...
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.moveaxis.html
numpy.moveaxis — NumPy v2.3 Manual
>>> import numpy as np >>> x = np.zeros((3, 4, 5)) >>> np.moveaxis(x, 0, -1).shape (4, 5, 3) >>> np.moveaxis(x, -1, 0).shape (5, 3, 4)
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_moveaxis_function.htm
Numpy moveaxis() Function
In the following example, we have moved axis 0 to axis 2 in a 3D numpy array using numpy.moveaxis() function − · import numpy as np my_Array = np.array([[[71, 44], [25, 100]], [[165, 32], [12, 1]]]) Moved_Array = np.moveaxis(my_Array, 0, 2) print("Original Array:\n", my_Array) print("Moved Axes Array (0 to 2):\n", Moved_Array)
🌐
MindSpore
mindspore.cn › docs › en › r2.6.0rc1 › api_python › numpy › mindspore.numpy.moveaxis.html
mindspore.numpy.moveaxis | MindSpore 2.6.0-rc1 documentation | MindSpore
>>> import mindspore.numpy as np >>> x = np.zeros((3, 4, 5)) >>> output = np.moveaxis(x, 0, -1) >>> print(output.shape) (4, 5, 3) >>> output = np.moveaxis(x, -1, 0) >>> print(output.shape) (5, 3, 4) >>> output = np.moveaxis(x, [0, 1, 2], [-1, -2, -3]) >>> print(output.shape) (5, 4, 3)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.moveaxis.html
numpy.moveaxis — NumPy v2.5 Manual
>>> import numpy as np >>> x = np.zeros((3, 4, 5)) >>> np.moveaxis(x, 0, -1).shape (4, 5, 3) >>> np.moveaxis(x, -1, 0).shape (5, 3, 4)
🌐
Python Forum
python-forum.io › thread-42539.html
Difficulty understanding .moveaxis in Numpy
August 5, 2024 - I am having difficulties understanding .moveaxis in Numpy. I first create an array by using a=np.arange(24).reshape(2,3,4). The system will first fill up axis 2 with 0 1 2 3, then move along axis 1 to the next row. When the first 'page' is done, the...
🌐
Pydocs
pydocs.github.io › p › numpy › 1.22.4 › api › numpy.moveaxis.html
Document
ParametersReturnsBackRef moveaxis(a, source, destination) Other axes remain in their original order. versionadded · a : np.ndarray · The array whose axes should be reordered. source : int or sequence of int · Original positions of the axes to move. These must be unique.
🌐
Spark Code Hub
sparkcodehub.com › numpy › data-manipulation › move-axis
Mastering Array Axis Moving in NumPy: A Comprehensive Guide
np.moveaxis function in NumPy repositions one or more specified axes of an array to new locations in the shape tuple, reordering its dimensions while preserving the data.
🌐
NumPy
numpy.org › doc › stable › search.html
Search - NumPy v2.4 Manual
Created using Sphinx 7.2.6 · Built with the PyData Sphinx Theme 0.16.1
🌐
Stack Overflow
stackoverflow.com › questions › 66431677
numpy - Python reshaping an array - Stack Overflow
np.moveaxis(array, axis_source, axis_destination): move axes of an array to new positions.