22.5 C
London
Tuesday, June 17, 2025
HomePandas in PythonGeneral Functions in PythonHow to Remove Duplicate Elements from NumPy Array

How to Remove Duplicate 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 duplicate elements in NumPy:

Method 1: Remove Duplicate Elements from NumPy Array

new_data = np.unique(data)

Method 2: Remove Duplicate Rows from NumPy Matrix

new_data = np.unique(data, axis=0)

Method 3: Remove Duplicate Columns from NumPy Matrix

new_data = np.unique(data, axis=1)

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

Example 1: Remove Duplicate Elements from NumPy Array

The following code shows how to remove duplicate elements from a NumPy array:

import numpy as np

#create NumPy array
data = np.array([1, 1, 1, 2, 2, 4, 5, 5, 5, 5, 7, 8])

#create new array that removes duplicates
new_data = np.unique(data)

#view new array
print(new_data)

[1 2 4 5 7 8]

Notice that all duplicates have been removed from the NumPy array and only unique values remain.

Example 2: Remove Duplicate Rows from NumPy Matrix

The following code shows how to remove duplicate rows from a NumPy matrix:

import numpy as np

#create NumPy matrix
data = np.array([[1, 5, 5, 8],
                 [1, 5, 5, 8],
                 [6, 2, 3, 4],
                 [6, 2, 3, 4]])

#create new array that removes duplicate rows
new_data = np.unique(data, axis=0)

#view new matrix
print(new_data)

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

Notice that all duplicate rows have been removed from the NumPy matrix and only unique rows remain.

Example 3: Remove Duplicate Columns from NumPy Matrix

The following code shows how to remove duplicate columns from a NumPy matrix:

import numpy as np

#create NumPy matrix
data = np.array([[1, 1, 5, 8, 1],
                 [1, 1, 2, 6, 1],
                 [4, 4, 3, 8, 4]])

#create new matrix that removes duplicate columns
new_data = np.unique(data, axis=1)

#view new matrix
print(new_data)

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

Notice that all duplicate columns have been removed from the NumPy matrix and only unique columns remain.

Additional Resources

The following tutorials explain how to perform other common tasks in NumPy:

How to Fill NumPy Array with Values
How to Remove Specific Elements from NumPy Array
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