
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swap Columns of a Given NumPy Array
When it comes to manipulating NumPy arrays, there might be instances where you want to interchange the positions of two columns. In this article, we delve into four distinct techniques to exchange columns in a given NumPy array: utilizing advanced indexing, employing NumPy indexing, harnessing the np.swapaxes function, and leveraging direct assignment. We will comprehend these methods through illustrative examples.
Method 1: Exploiting Advanced Indexing
Unleashing the potential of advanced indexing, you open the capability to reshape the course of action of measurements inside a NumPy cluster, much obliged to a choosy curated program of column indices.
Example
In the following example, we generate a NumPy array and employ advanced indexing to exchange columns 1 and 2.
import numpy as np array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) array[:, [1, 2]] = array[:, [2, 1]] print(array)
Output
[[1 3 2] [4 6 5] [7 9 8]]
Method 2: Employing NumPy Indexing
NumPy indexing permits you to create a new array with the desired column sequence by specifying the indices of the columns.
Example
In the ensuing illustration, we fabricate a NumPy array and employ the prowess of NumPy indexing to interchange columns 1 and 2.
import numpy as np array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) array = array[:, [0, 2, 1]] print(array)
Output
[[1 3 2] [4 6 5] [7 9 8]]
Method 3: Harnessing the np.swapaxes Function
The np.swapaxes function can be employed to exchange two axes of a NumPy array. To interchange columns, you can initially transpose the array, exchange the rows, and then transpose it back.
Example
In the ensuing example, we generate a NumPy array and harness the np.swapaxes function to exchange columns 1 and 2.
import numpy as np array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) array = array.T array[[1, 2]] = array[[2, 1]] array = array.T print(array)
Output
[[1 3 2] [4 6 5] [7 9 8]]
Method 4: Leveraging Direct Assignment
Direct assignment can be employed to exchange columns in a NumPy array by creating a temporary copy of one of the columns.
Example
In the following example, we generate a NumPy array and leverage direct assignment to exchange columns 1 and 2.
import numpy as np array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) temp = array[:, 1].copy() array[:, 1] = array[:, 2] array[:, 2] = temp print(array)
Output
[[1 3 2] [4 6 5] [7 9 8]]
Conclusion
Throughout this article, we have explored different methods for swapping columns in a NumPy array. We discussed advanced indexing, NumPy indexing, the np.swapaxes function, and direct assignment. Each method bestows upon you a straightforward and highly efficient means to swap columns, catering to the idiosyncrasies of your specific requirements. By assimilating the syntax and perusing the examples of these techniques, you gain the ability to handpick the most fitting approach tailored to your data manipulation endeavors.
In culmination, the aptitude to shuffle columns within a NumPy array manifests as an invaluable dexterity for the realm of data manipulation and analysis.