unique and other set logic#

NumPy has some basic set operations for one-dimensional ndarray. A commonly used one is numpy.unique, which returns the sorted unique values in an array:

import numpy as np

names = np.array(
array(['Ava', 'Emma', 'Liam', 'Noah', 'Oliver', 'Olivia'], dtype='<U6')

With numpy.in1d you can check the membership of the values in a one-dimensional array to another array and a boolean array is returned:

np.in1d(names, ["Emma", "Ava", "Charlotte"])
array([False, False, False, False, False, False, False,  True, False,

Array set operations:




calculates the sorted, unique elements in x

intersect1d(x, y)

calculates the sorted common elements x and y

union1d(x, y)

calculates the sorted union of elements

in1d(x, y)

computes a boolean array indicating whether each element of x is contained in y

setdiff1d(x, y)

sets the difference of the elements in x that are not contained in y

setxor1d(x, y)

sets symmetric differences; elements contained in one of the arrays but not in both