3.1 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Remove Specific Elements from NumPy Array

How to Remove Specific Elements from NumPy Array

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following methods to remove specific elements from a NumPy array:

Method 1: Remove Elements Equal to Specific Value

#remove elements whose value is equal to 12
new_array = np.delete(original_array, np.where(original_array == 12))

Method 2: Remove Elements Equal to Some Value in List

#remove elements whose value is equal to 2, 5, or 12
new_array = np.setdiff1d(original_array, [2, 5, 12])

Method 3: Remove Elements Based on Index Position

#remove elements in index positions 0 and 6
new_array = np.delete(original_array, [0, 6])

The following examples show how to use each method in practice.

Example 1: Remove Elements Equal to Specific Value

The following code shows how to remove all elements from a NumPy array whose value is equal to 12:

import numpy as np

#define original array of values
original_array = np.array([1, 2, 2, 4, 5, 7, 9, 12, 12])

#remove elements whose value is equal to 12
new_array = np.delete(original_array, np.where(original_array == 12))

#view new array
print(new_array)

[1 2 2 4 5 7 9]

Notice that both elements in the array that were equal to 12 have been removed.

Example 2: Remove Elements Equal to Some Value in List

The following code shows how to remove all elements from a NumPy array whose values is equal to 2, 5, or 12:

import numpy as np

#define original array of values
original_array = np.array([1, 2, 2, 4, 5, 7, 9, 12, 12])

#remove elements whose value is equal to 2, 5, or 12
new_array = np.setdiff1d(original_array, [2, 5, 12])

#view new array
print(new_array)

[1 4 7 9]

Notice that all elements whose value was 2, 5, or 12 have been removed.

Example 3: Remove Elements Based on Index Position

The following code shows how to remove the elements in index positions 0 and 6 from a NumPy array:

import numpy as np

#define original array of values
original_array = np.array([1, 2, 2, 4, 5, 7, 9, 12, 12])

#remove elements in index positions 0 and 6
new_array = np.delete(original_array, [0, 6])

#view new array
print(new_array)

[ 2  2  4  5  7 12 12]

Notice that the elements in index position 0 (with value of 1) and index position 6 (with value of 9) have both been removed from the NumPy array.

Additional Resources

The following tutorials explain how to perform other common operations in Python:

How to Fill NumPy Array with Values
How to Replace Elements in NumPy Array
How to Get Specific Row from NumPy Array

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories