Extract Specific Columns in NumPy Array (3 Best Ways)

In this tutorial, we will learn how to extract specific columns from a NumPy array using the different approaches? By Pranit Sharma Last updated : May 28, 2023

Problem Statement

Suppose we are given a 2D NumPy array (M x N) matrix, we need to extract specific columns and store them in another NumPy array.

How to extract specific columns in NumPy array?

There can be multiple approaches by using them you can extract specific columns such as slicing, numpy.ix_() method, ellipsis or three dots (...), and many more. Here, we're discussing these three approaches.

Approach 1: Using Slicing

The standard slicing technique with a NumPy array can be used to extract specific columns. For example, you want to extract columns 1 and 3. Follow the below-given syntax:

res = arr[:, [1, 3]]

Here, arr is the name of the input array and res is the subarray in which the result will be stored.

Example 1: Extract specific NumPy array's columns using slicing

# Import numpy
import numpy as np

# Creating a numpy 2D array
arr = np.array([[1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]])

# Printing original array
print("Original array:\n", arr, "\n")

# Extracting specific columns
# using slicing approach
res = arr[:, [1, 3]]

# Printing specific columns
print("Specific columns:\n", res)

Output

Original array:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]] 

Specific columns:
 [[ 2  4]
 [ 6  8]
 [10 12]
 [14 16]]

Approach 2: Using numpy.ix_() Method

The numpy.ix()_ method is used to construct an open mesh from multiple sequences. You can use numpy.ix()_ method for extracting specific columns from a NumPy array. For that, you have to specify the list of rows and columns. For example, you want to extract columns 1 and 3 of all rows. Follow the below-given syntax:

res = arr[np.ix_([0, 1, 2, 3], [1, 3])]

Here, arr is the name of the input array and res is the subarray in which the result will be stored. Inside numpy.ix()_ method, [0, 1, 2, 3] is the indices of rows and [1, 3] is the indices of columns.

Example 2: Extract specific NumPy array's columns using numpy.ix()_ method

# Import numpy
import numpy as np

# Creating a numpy 2D array
arr = np.array([[1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]])

# Printing original array
print("Original array:\n", arr, "\n")

# Extracting specific columns
# using numpy.ix()_ method
# Specify rows and columns
res = arr[np.ix_([0, 1, 2, 3], [1, 3])]

# Printing specific columns
print("Specific columns:\n", res)

Output

Original array:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]] 

Specific columns:
 [[ 2  4]
 [ 6  8]
 [10 12]
 [14 16]]

Approach 3: Using Ellipsis or Three Dots (...)

Ellipsis or three dots (...) is a singleton Python object which has no method. You can use ellipsis to extract specific columns. For example, you want to extract columns 1 and 3 of all rows. Follow the below-given syntax:

res = arr[..., 1:3]

Here, arr is the name of the input array and res is the subarray in which the result will be stored.

Example 3: Extract specific NumPy array's columns using Ellipsis or Three Dots (...)

# Import numpy
import numpy as np

# Creating a numpy 2D array
arr = np.array([[1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]])

# Printing original array
print("Original array:\n", arr, "\n")

# Extracting specific columns
# using using Ellipsis
res = arr[..., 1:3]

# Printing specific columns
print("Specific columns:\n", res)

Output

Original array:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]] 

Specific columns:
 [[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.