How can two rows be swapped in a NumPy array? Provide an example.

How can two rows be swapped in a NumPy array? Provide an example.

Swapping two rows in a NumPy array can be accomplished using the function np.swapaxes(). This function takes in the array as the first argument, followed by the two row indices to be swapped. For example, if we have an array arr = np.array([[1,2,3],[4,5,6],[7,8,9]]), we can swap the first and second rows by calling np.swapaxes(arr, 0, 1). This will result in the array becoming np.array([[4,5,6],[1,2,3],[7,8,9]]), with the first and second rows being swapped. This function is useful for rearranging data in arrays and can be used for any number of dimensions.

Swap Two Rows in a NumPy Array (With Example)


You can use the following basic syntax to swap two rows in a NumPy array:

some_array[[0, 3]] = some_array[[3, 0]]

This particular example will swap the first and fourth rows in the NumPy array called some_array.

All other rows will remain in their original positions.

The following example shows how to use this syntax in practice.

Example: Swap Two Rows in NumPy Array

Suppose we have the following NumPy array:

import numpy as np

#create NumPy array
some_array = np.array([[1, 1, 2], [3, 3, 7], [4, 3, 1], [9, 9, 5], [6, 7, 7]])

#view NumPy array
print(some_array)

[[1 1 2]
 [3 3 7]
 [4 3 1]
 [9 9 5]
 [6 7 7]]

We can use the following syntax to swap the first and fourth rows in the NumPy array:

#swap rows 1 and 4
some_array[[0, 3]] = some_array[[3, 0]]

#view updated NumPy array
print(some_array)

[[9 9 5]
 [3 3 7]
 [4 3 1]
 [1 1 2]
 [6 7 7]]

Notice that the first and fourth rows have been swapped.

All other rows remained in their original positions.

Note that some_array[[0,  3]] is shorthand for some_array[[0, 3],  :] so we could also use the following syntax to get the same results:

#swap rows 1 and 4
some_array[[0, 3], :] = some_array[[3, 0], :]

#view updated NumPy array
print(some_array)

[[9 9 5]
 [3 3 7]
 [4 3 1]
 [1 1 2]
 [6 7 7]]

Notice that the first and fourth rows have been swapped.

This result matches the result from using the shorthand notation in the previous example.

Feel free to use whichever notation you prefer to swap two rows in a given NumPy array.

The following tutorials explain how to perform other common tasks in NumPy:

Cite this article

stats writer (2024). How can two rows be swapped in a NumPy array? Provide an example.. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-two-rows-be-swapped-in-a-numpy-array-provide-an-example/

stats writer. "How can two rows be swapped in a NumPy array? Provide an example.." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-two-rows-be-swapped-in-a-numpy-array-provide-an-example/.

stats writer. "How can two rows be swapped in a NumPy array? Provide an example.." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-two-rows-be-swapped-in-a-numpy-array-provide-an-example/.

stats writer (2024) 'How can two rows be swapped in a NumPy array? Provide an example.', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-two-rows-be-swapped-in-a-numpy-array-provide-an-example/.

[1] stats writer, "How can two rows be swapped in a NumPy array? Provide an example.," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can two rows be swapped in a NumPy array? Provide an example.. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top