You can use numpy.random.shuffle().

This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

In [2]: import numpy as np                                                                                                                                                                                  

In [3]:                                                                                                                                                                                                     

In [3]: X = np.random.random((6, 2))                                                                                                                                                                        

In [4]: X                                                                                                                                                                                                   
Out[4]: 
array([[0.71935047, 0.25796155],
       [0.4621708 , 0.55140423],
       [0.22605866, 0.61581771],
       [0.47264172, 0.79307633],
       [0.22701656, 0.11927993],
       [0.20117207, 0.2754544 ]])

In [5]: np.random.shuffle(X)                                                                                                                                                                                

In [6]: X                                                                                                                                                                                                   
Out[6]: 
array([[0.71935047, 0.25796155],
       [0.47264172, 0.79307633],
       [0.4621708 , 0.55140423],
       [0.22701656, 0.11927993],
       [0.20117207, 0.2754544 ],
       [0.22605866, 0.61581771]])

For other functionalities you can also check out the following functions:

  • random.Generator.shuffle

  • random.Generator.permutation

  • random.Generator.permuted

The function random.Generator.permuted is introduced in Numpy's 1.20.0 Release.

The new function differs from shuffle and permutation in that the subarrays indexed by an axis are permuted rather than the axis being treated as a separate 1-D array for every combination of the other indexes. For example, it is now possible to permute the rows or columns of a 2-D array.

Answer from Kasravnd on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.2 › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v2.2 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([[3, 4, 5], # random [6, 7, 8], [0, 1, 2]])
🌐
NumPy
numpy.org › doc › stable › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v2.4 Manual
>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8] # random · Multi-dimensional arrays are only shuffled along the first axis:
🌐
NumPy
numpy.org › doc › 2.1 › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v2.1 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([[3, 4, 5], # random [6, 7, 8], [0, 1, 2]])
🌐
NumPy
numpy.org › devdocs › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v2.5.dev0 Manual
>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8] # random · Multi-dimensional arrays are only shuffled along the first axis:
🌐
Codecademy
codecademy.com › docs › python:numpy › random module › .shuffle()
Python:NumPy | Random Module | .shuffle() | Codecademy
May 19, 2025 - In Numpy random module, the .shuffle() function randomly rearranges the elements of an array or sequence. It modifies the original array in place, altering its contents directly. ... Looking for an introduction to the theory behind programming?
🌐
DataCamp
datacamp.com › doc › numpy › random-shuffle
NumPy random.shuffle()
In this syntax, `x` is the array you want to shuffle. The operation is performed in-place, meaning the original array is modified. import numpy as np arr = np.array([1, 2, 3, 4, 5]) np.random.shuffle(arr) print(arr)
🌐
NumPy
numpy.org › doc › 2.2 › reference › random › generated › numpy.random.Generator.shuffle.html
numpy.random.Generator.shuffle — NumPy v2.2 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> arr array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> rng.shuffle(arr, axis=1) >>> arr array([[2, 0, 1], # random [5, 3, 4], [8, 6, 7]])
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-random-shuffle-in-python
numpy.random.shuffle() in python - GeeksforGeeks
August 22, 2025 - For one-dimensional arrays, it behaves like Python's built-in random.shuffle() but works with NumPy arrays.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.0 › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v2.0 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([[3, 4, 5], # random [6, 7, 8], [0, 1, 2]])
Top answer
1 of 5
88

You can use numpy.random.shuffle().

This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

In [2]: import numpy as np                                                                                                                                                                                  

In [3]:                                                                                                                                                                                                     

In [3]: X = np.random.random((6, 2))                                                                                                                                                                        

In [4]: X                                                                                                                                                                                                   
Out[4]: 
array([[0.71935047, 0.25796155],
       [0.4621708 , 0.55140423],
       [0.22605866, 0.61581771],
       [0.47264172, 0.79307633],
       [0.22701656, 0.11927993],
       [0.20117207, 0.2754544 ]])

In [5]: np.random.shuffle(X)                                                                                                                                                                                

In [6]: X                                                                                                                                                                                                   
Out[6]: 
array([[0.71935047, 0.25796155],
       [0.47264172, 0.79307633],
       [0.4621708 , 0.55140423],
       [0.22701656, 0.11927993],
       [0.20117207, 0.2754544 ],
       [0.22605866, 0.61581771]])

For other functionalities you can also check out the following functions:

  • random.Generator.shuffle

  • random.Generator.permutation

  • random.Generator.permuted

The function random.Generator.permuted is introduced in Numpy's 1.20.0 Release.

The new function differs from shuffle and permutation in that the subarrays indexed by an axis are permuted rather than the axis being treated as a separate 1-D array for every combination of the other indexes. For example, it is now possible to permute the rows or columns of a 2-D array.

2 of 5
30

You can also use np.random.permutation to generate random permutation of row indices and then index into the rows of X using np.take with axis=0. Also, np.take facilitates overwriting to the input array X itself with out= option, which would save us memory. Thus, the implementation would look like this -

np.take(X,np.random.permutation(X.shape[0]),axis=0,out=X)

Sample run -

In [23]: X
Out[23]: 
array([[ 0.60511059,  0.75001599],
       [ 0.30968339,  0.09162172],
       [ 0.14673218,  0.09089028],
       [ 0.31663128,  0.10000309],
       [ 0.0957233 ,  0.96210485],
       [ 0.56843186,  0.36654023]])

In [24]: np.take(X,np.random.permutation(X.shape[0]),axis=0,out=X);

In [25]: X
Out[25]: 
array([[ 0.14673218,  0.09089028],
       [ 0.31663128,  0.10000309],
       [ 0.30968339,  0.09162172],
       [ 0.56843186,  0.36654023],
       [ 0.0957233 ,  0.96210485],
       [ 0.60511059,  0.75001599]])

Additional performance boost

Here's a trick to speed up np.random.permutation(X.shape[0]) with np.argsort() -

np.random.rand(X.shape[0]).argsort()

Speedup results -

In [32]: X = np.random.random((6000, 2000))

In [33]: %timeit np.random.permutation(X.shape[0])
1000 loops, best of 3: 510 µs per loop

In [34]: %timeit np.random.rand(X.shape[0]).argsort()
1000 loops, best of 3: 297 µs per loop

Thus, the shuffling solution could be modified to -

np.take(X,np.random.rand(X.shape[0]).argsort(),axis=0,out=X)

Runtime tests -

These tests include the two approaches listed in this post and np.shuffle based one in @Kasramvd's solution.

In [40]: X = np.random.random((6000, 2000))

In [41]: %timeit np.random.shuffle(X)
10 loops, best of 3: 25.2 ms per loop

In [42]: %timeit np.take(X,np.random.permutation(X.shape[0]),axis=0,out=X)
10 loops, best of 3: 53.3 ms per loop

In [43]: %timeit np.take(X,np.random.rand(X.shape[0]).argsort(),axis=0,out=X)
10 loops, best of 3: 53.2 ms per loop

So, it seems using these np.take based could be used only if memory is a concern or else np.random.shuffle based solution looks like the way to go.

🌐
NumPy
numpy.org › doc › 2.3 › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v2.3 Manual
>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8] # random · Multi-dimensional arrays are only shuffled along the first axis:
🌐
NumPy
numpy.org › doc › stable › reference › random › generated › numpy.random.Generator.shuffle.html
numpy.random.Generator.shuffle — NumPy v2.4 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> arr array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> rng.shuffle(arr, axis=1) >>> arr array([[2, 0, 1], # random [5, 3, 4], [8, 6, 7]])
🌐
NumPy
numpy.org › doc › 1.25 › reference › random › generated › numpy.random.shuffle.html
numpy.random.shuffle — NumPy v1.25 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([[3, 4, 5], # random [6, 7, 8], [0, 1, 2]])
🌐
Quora
quora.com › How-does-one-shuffle-an-array-in-numpy-in-a-reproducable-manner
How does one shuffle an array in numpy in a reproducable manner? - Quora
Once you have that, you can apply a Fisher–Yates shuffle to the array. ... Randomly select an array element in the range starting at the current position and stretching to the end of the array with a uniform integer distribution.
🌐
Like Geeks
likegeeks.com › home › python › numpy › 11 amazing numpy shuffle examples
11 Amazing NumPy Shuffle Examples
July 6, 2024 - Note that this method does not perform in-place shuffling like np.random.shuffle does, instead, it returns the shuffled array without modifying the input arrays. Since this method does not involve in-place item reassignment, we can also shuffle immutable iterables using this method. We have seen the effect of NumPy’s shuffle method on 1-dimensional array.
🌐
GeeksforGeeks
geeksforgeeks.org › python › shuffle-an-array-in-python
Shuffle an array in Python - GeeksforGeeks
July 23, 2025 - Here we are using the shuffle() method from numpy library to shuffle an array in Python.
🌐
NumPy
numpy.org › doc › 2.1 › reference › random › generated › numpy.random.Generator.shuffle.html
numpy.random.Generator.shuffle — NumPy v2.1 Manual
>>> arr = np.arange(9).reshape((3, 3)) >>> arr array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> rng.shuffle(arr, axis=1) >>> arr array([[2, 0, 1], # random [5, 3, 4], [8, 6, 7]])
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-203.php
NumPy: Shuffle specific rows of a given array - w3resource
August 29, 2025 - # Importing NumPy library import numpy as np # Setting seed for reproducibility np.random.seed(42) # Creating a NumPy array containing student data student = np.array([ ['stident_id', 'Class', 'Name'], ['01', 'V', 'Debby Pramod'], ['02', 'V', 'Artemiy Ellie'], ['03', 'V', 'Baptist Kamal'], ['04', 'V', 'Lavanya Davide'], ['05', 'V', 'Fulton Antwan'], ['06', 'V', 'Euanthe Sandeep'], ['07', 'V', 'Endzela Sanda'], ['08', 'V', 'Victoire Waman'], ['09', 'V', 'Briar Nur'], ['10', 'V', 'Rose Lykos'] ]) # Displaying the original array print("Original array:") print(student) # Shuffling rows from index 2 to 7 (3rd to 8th rows) np.random.shuffle(student[2:8]) # Displaying the shuffled array print("Shuffle the said array rows starting from 3rd to 9th") print(student)
🌐
Medium
medium.com › @heyamit10 › what-is-numpy-random-shuffle-and-when-to-use-it-694028970fe7
What is numpy.random.shuffle and When to Use It? | by Hey Amit | Medium
February 8, 2025 - Think of shuffle as someone who rearranges your furniture right there in your living room — no duplicates, no extra copies, just reorganization. On the other hand, permutation creates a whole new room with rearranged furniture, leaving the original untouched. ... import numpy as np arr = np.array([1, 2, 3, 4, 5]) # Using shuffle np.random.shuffle(arr) print("After shuffle:", arr) # Original array is modified # Using permutation arr = np.array([1, 2, 3, 4, 5]) shuffled = np.random.permutation(arr) print("Original array:", arr) # Original array is unchanged print("Shuffled array:", shuffled)
🌐
Moonbooks
moonbooks.org › Articles › How-to-randomly-shuffle-an-array-in-python-using-numpy
How to randomly shuffle an array in python using numpy
August 24, 2022 - >>> import numpy as np >>> M = np.array([4,8,15,16,23,42]) >>> np.random.shuffle(M) >>> M array([15, 16, 8, 42, 23, 4]) >>> np.random.shuffle(M) >>> M array([ 8, 42, 23, 15, 16, 4]) >>>