How to Remove Columns from NumPy Array

In this article, we will learn how to remove single or multiple columns from a 2D NumPy Array in Python.

Table of Contents

Given a 2D NumPy array we need to delete the specified columns from it. For Example:

Given 2D NumPy Array:

[[1, 2, 3, 4, 5],
 [5, 4, 3, 2, 1],
 [8, 2, 4, 1, 5], 
 [3, 4, 3, 2, 1], 
 [7, 6, 3, 4, 5]]

After removing 2nd Column:

Advertisements
[[1, 3, 4, 5],
 [5, 3, 2, 1],
 [8, 4, 1, 5], 
 [3, 3, 2, 1], 
 [7, 3, 4, 5]]

There are multiple ways to delete columns from a NumPy Array. Lets discuss all the methods one by one with proper approach and a working code example.

1.) Remove Single Column from NumPy Array using delete()

The delete() method is a built-in method in numpy library and it provides a way to delete the elements from the given array. The delete() method takes an array and an index position or array of indices as parameters. It returns an array by deleting the elements at given index.

Now to delete a column from a NumPy Array, we need to pass the given array and the index of the column and axis=1 to the delete() method. In this example, to delete the 2nd column from the given 2D Array, call the function delete(a, 1, axis=1) . Here, index position of 2nd column is 1.

Syntax of delete()

numpy.delete(arr, obj, axis)

Parameters:

        arr          = The array to be passed to the function.
        obj          = index (or array of index)  of the rows or columns to be deleted.
        axis         = Axis along which we want to delete. 
                       If axis is 1 then delete columns, if axis is 0 then delete rows.
                       If None then flatten the array and then apply delete on it.

Return:

If axis is 1, then it returns an array with the specified columns removed.

Approach

1. Import numpy library and create numpy array
2. Now pass the given array and the index of column to be removed to the delete() method.
3. Set the axis = 1 in the delete() method.
4. The method will return the array with the specified column deleted.
5. print the array.

Source code

import numpy as np

# Create 2D NumPy Array
a = np.array([[1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1],
              [8, 2, 4, 1, 5],
              [3, 4, 3, 2, 1]])

print('Original Array:')
print(a)

# Delete 2nd column from the 2D NumPy Array
# passing index as 1 and setting axis=1
a = np.delete(a, 1, axis=1)

print('After removing 2nd column of Array:')
print(a)

OUTPUT:

Original Array:

[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]]


After removing 2nd column of Array:

[[1 3 4 5]
 [5 3 2 1]
 [8 4 1 5]
 [3 3 2 1]]

2.) Remove Multiple Columns from NumPy Array using delete()

The delete() method is a built-in method in numpy library and it is used to remove the columns from the given 2D NumPy Array.

numpy.delete(arr, obj, axis)

The delete() method takes an array and a index position or array of index positions as parameters. It returns an array by deleting the elements at given index or indices. Now to remove multiple column in the array we need to pass the given array and the ‘array of indices’ of the columns to be deleted and axis=1 to the delete() method. In this example, lets delete the 1st and 2nd column from the given array. For that we can call delete() function with following arguments –> delete(a, [0,1], axis=1). Here, we passed list of indexes of 1st and 2nd column along with axis 1 to the delete() function.

Approach

1. Import numpy library and create numpy array
2. Now use pass the given array and the array of index of columns to be deleted to the delete() method.
3. Set the axis = 1 in the delete() method.
4. The method will return the array with the specified columns deleted.
5. Print the array.

Source code

import numpy as np

# Create 2D NumPy Array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1],
                [8, 2, 4, 1, 5],
                [3, 4, 3, 2, 1]])

print('Original Array:')
print(arr)

# Delete 1st and 2nd columns from the 2D NumPy Array
# passing index as 1 and setting axis=1
arr = np.delete(arr, [0,1], axis=1)

print('After removing 1st and 2nd columns of Array:')
print(arr)

OUTPUT:

Original Array:

[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]]


After removing 1st and 2nd columns of Array:

[[3 4 5]
 [3 2 1]
 [4 1 5]
 [3 2 1]]

3.) Remove Multiple Columns from NumPy Array using slicing

Slicing in python can be defined as selecting elements from array by range i.e. selecting from one given index to another given index position.

Example:             
        arr = [ 1, 3, 5, 8, 9 ]
        arr[ start : stop ]
        arr[0 : 2]  ===> this will give [ 1, 3], The stop index will be excluded.
    

The delete() method is a built-in method in numpy library and it is used to remove the columns from the given array. The delete() method takes an array and an index position or array of index parameters. It returns an array by deleting the elements at given index or indices.
Now to remove multiple columns in the array we need to pass the given array and the ‘array of index’ of the columns to be deleted and axis=1 to the delete() method. Slice() method is used to pass a slice of column indices to the delete() method.

Syntax of slice()

slice(start, end)

Parameters:

start        = The start of index
end          = The end of index, last index will be excluded.

In this example let’s delete the 1st column and 2nd column from the given array. For that we will call delete(a, slice(0,3), axis=1). Pass the indices from 1st to 3rd column in delete() function. This will delete the 1st and 2nd column because the index of 3rd column is excluded in slicing.

Approach

1. Import numpy library and create numpy array
2. Create a slice object using slice()
2. Now use pass the given array and the slice object to the delete() method.
3. Set the axis = 1 in the delete() method.
4. The method will return the array with the specified columns deleted.
5. print the array.

Source code

import numpy as np

# Create 2D NumPy Array
a = np.array([[1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1],
              [8, 2, 4, 1, 5],
              [3, 4, 3, 2, 1]])

print('Original Array:')
print(a)

# deletes the first 2 columns from 2D Array
a = np.delete(a, slice(0,2), axis=1)

print('After removing first two columns of NumPy Array:')
print(a)

OUTPUT:

Original Array:

[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]]


After removing first two columns of NumPy Array:

[[3 4 5]
 [3 2 1]
 [4 1 5]
 [3 2 1]]

4.) Remove Multiple Columns from NumPy Array using Boolean Array

The columns in a 2D NumPy Array can be accessed by passing a Boolean array as an index to the array

Example:

arr = [ [1, 2, 3, 4, 5],
        [5, 4, 3, 2, 1],
        [8, 2, 4, 1, 5],
        [3, 4, 3, 2, 1],
        [7, 6, 3, 4, 5]]
             
boolArray = [True, True, False, False, False]

arr[:,boolArray]  ===> this will give,

[[1, 2],
 [5, 4],
 [8, 2],
 [3, 4], 
 [7, 6]]
    

Now to remove columns from the 2D NumPy Array, create a Boolean array with length same as number of columns in the 2D NumPy Array. For columns to be deleted, set False at corresponding index in Boolean array and True at other positions. Pass this Boolean array in the index range of the subscript operator of the given array i.e. arr[ : ,boolArr]. This will return an array with the specified columns deleted.

Approach

  • Import numpy library and create numpy array
  • Create a Boolean array with length same as number of columns in 2D NumPy Array.
  • For columns to be deleted, set False at corresponding index in Boolean array and True at other positions.
  • Now Pass this Boolean array as index to the given array i.e. arr[ : ,boolArr]
  • This will give an array with the specified columns deleted.

Source code

import numpy as np

# Create 2D NumPy Array
arr = np.array([[1, 2, 3, 4, 5],
                [5, 4, 3, 2, 1],
                [8, 2, 4, 1, 5],
                [3, 4, 3, 2, 1],
                [7, 6, 3, 4, 5]])

print('Original Array:')
print(arr)

# Removing the columns using boolean index
booleanIndex = [True, True, True,False, True]
arr = arr[:,booleanIndex]

print('After removing the 4th column of 2D Array:')
print(arr)

OUTPUT:

Original Array:

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


After removing the 4th column of 2D Array:

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

Summary

Great! you made it. We have discussed all possible methods to delete columns from a NumPy Array. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top