Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to access a NumPy array by column?
When working with datasets in Python, accessing specific columns of a NumPy array is a fundamental operation for data analysis and manipulation. NumPy provides several powerful methods to access columns efficiently.
In this article, we will explore different techniques to access columns in a NumPy array, from basic indexing to advanced boolean filtering.
Method 1: Basic Indexing
Basic indexing is the simplest way to access a column. Use the colon ":" operator to select all rows and specify the column index ?
Example
import numpy as np
# Create a sample NumPy array
array = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
# Access the third column (index 2)
column = array[:, 2]
print("Third column:")
print(column)
The output of the above code is ?
Third column: [ 3 7 11]
Method 2: Fancy Indexing
Fancy indexing allows you to access multiple columns simultaneously by passing an array of column indices ?
Example
import numpy as np
# Create a sample NumPy array
array = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
# Access columns at indices 1 and 3
columns = array[:, [1, 3]]
print("Columns 1 and 3:")
print(columns)
The output of the above code is ?
Columns 1 and 3: [[ 2 4] [ 6 8] [10 12]]
Method 3: Boolean Indexing
Boolean indexing allows you to select columns based on specific conditions using a boolean mask ?
Example
import numpy as np
# Create a sample NumPy array
array = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
# Select columns where the sum is greater than 15
condition = array.sum(axis=0) > 15
columns_filtered = array[:, condition]
print("Columns with sum > 15:")
print(columns_filtered)
print("Column sums:", array.sum(axis=0))
The output of the above code is ?
Columns with sum > 15: [[ 2 3 4] [ 6 7 8] [10 11 12]] Column sums: [15 18 21 24]
Method 4: Using Array Transpose
Transposing swaps rows and columns, allowing you to access columns as rows using the .T attribute ?
Example
import numpy as np
# Create a sample NumPy array
array = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
# Transpose and access the third column (now third row)
transposed = array.T
column = transposed[2]
print("Third column via transpose:")
print(column)
The output of the above code is ?
Third column via transpose: [ 3 7 11]
Comparison
| Method | Use Case | Returns |
|---|---|---|
| Basic Indexing | Single column access | 1D array |
| Fancy Indexing | Multiple specific columns | 2D array |
| Boolean Indexing | Conditional column selection | 2D array |
| Transpose | Column-as-row operations | 1D array |
Conclusion
NumPy offers multiple efficient ways to access array columns. Use basic indexing for single columns, fancy indexing for multiple columns, boolean indexing for conditional selection, and transpose for column-as-row operations.
