As an example:

```
import numpy as np
def shuffle_rows(arr, rows):
np.random.shuffle(arr[rows[0]: rows[1] + 1])
a = np.arange(20).reshape(4, 5)
print(a)
# array([
[0, 1, 2, 3, 4],
#[5, 6, 7, 8, 9],
#[10, 11, 12, 13, 14],
#[15, 16, 17, 18, 19]
])
shuffle_rows(a, [1, 3])
print(a)
#array([
[0, 1, 2, 3, 4],
#[10, 11, 12, 13, 14],
#[15, 16, 17, 18, 19],
#[5, 6, 7, 8, 9]
])
shuffle_rows(a, [1, 3])
print(a)
#array([
[0, 1, 2, 3, 4],
#[10, 11, 12, 13, 14],
#[5, 6, 7, 8, 9],
#[15, 16, 17, 18, 19]
])
```

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.,Multi-dimensional arrays are only shuffled along the first axis:,New code should use the shuffle method of a default_rng() instance instead; please see the Quick Start.,The array, list or mutable sequence to be shuffled.

>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr[1 7 5 2 9 4 3 6 0 8] # random

>>> arr = np.arange(9).reshape((3, 3)) >>> np.random.shuffle(arr) >>> arr array([ [3, 4, 5], # random[6, 7, 8], [0, 1, 2] ])

I want to shuffle the ordering of only some rows in a numpy array. These rows will always be continuous (e.g. shuffling rows 23-80). The number of elements in each row can vary from 1 (such that the array is actually 1D) to 100.,You can use np.random.shuffle. This shuffles the rows themselves, not the elements within the rows., 1 week ago Numpy shuffle multidimensional array by row only, keep column order unchanged. 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. , 2 days ago Jan 08, 2018 · numpy.random.shuffle. ¶. Modify a sequence in-place by shuffling its contents. 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. The array or list to be shuffled.

import numpy as np >>> a = np.arange(20).reshape(4, 5) >>> a array([ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19] ]) >>> shuffle_rows(a, [1, 3]) # including rows 1, 2 and 3 in the shuffling array([ [0, 1, 2, 3, 4], [15, 16, 17, 18, 19], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14] ])

```
import numpy as np def shuffle_rows(arr, rows): np.random.shuffle(arr[rows[0]: rows[1] + 1]) a = np.arange(20).reshape(4, 5) print(a) # array([
[0, 1, 2, 3, 4], #[5, 6, 7, 8, 9], #[10, 11, 12, 13, 14], #[15, 16, 17, 18, 19]
]) shuffle_rows(a, [1, 3]) print(a) #array([
[0, 1, 2, 3, 4], #[10, 11, 12, 13, 14], #[15, 16, 17, 18, 19], #[5, 6, 7, 8, 9]
]) shuffle_rows(a, [1, 3]) print(a) #array([
[0, 1, 2, 3, 4], #[10, 11, 12, 13, 14], #[5, 6, 7, 8, 9], #[15, 16, 17, 18, 19]
])
```

import numpy as np >>> a = np.arange(20).reshape(4, 5) >>> a array([ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19] ]) >>> shuffle_rows(a, [1, 3]) # including rows 1, 2 and 3 in the shuffling array([ [0, 1, 2, 3, 4], [15, 16, 17, 18, 19], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14] ])

```
import numpy as np def shuffle_rows(arr, rows): np.random.shuffle(arr[rows[0]: rows[1] + 1]) a = np.arange(20).reshape(4, 5) print(a) # array([
[0, 1, 2, 3, 4], #[5, 6, 7, 8, 9], #[10, 11, 12, 13, 14], #[15, 16, 17, 18, 19]
]) shuffle_rows(a, [1, 3]) print(a) #array([
[0, 1, 2, 3, 4], #[10, 11, 12, 13, 14], #[15, 16, 17, 18, 19], #[5, 6, 7, 8, 9]
]) shuffle_rows(a, [1, 3]) print(a) #array([
[0, 1, 2, 3, 4], #[10, 11, 12, 13, 14], #[5, 6, 7, 8, 9], #[15, 16, 17, 18, 19]
])
```

Last Updated : 05 Sep, 2021,GATE CS 2021 Syllabus

**Output**

Original array: [1 2 3 4 5 6] Shuffled array: [4 1 5 3 2 6]

**Output:**

Original array: [1 2 3 4 5 6] Shuffled array: [4 5 2 6 1 3]

`[2, 3, 6, 1, 5, 4]`

`[2, 3, 6, 1, 5, 4]`