How to Remove Duplicate Elements from NumPy Array


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

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.

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

x