Skip to Main Content

Python for Basic Data Analysis: NP.11 Finding unique elements and sorting

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

Finding unique elements in an array

Finding unique elements in an array

np.unique lets you find out the unique elements of an array. 

Sorting elements in arrays

Sorting elements in arrays

np.sort enables you to sort the elements in your array.

It can be used as a function of NumPy or as a method which will both create the same results. Below are examples of both methods. 

When used as a function, the original array does not change. When used as a method, the original array also changes. 

Sort by rows or columns

You can also sort the elements in rows or columns of a matrix by specifying the axis. 

Exercises

1. Find the unique elements in this array:

myArray_1 = np.array([[2,2,3,4],[3,2,3,4]])

 

2. Sort the elements in this array by its rows:

myArray_2 = np.array([[2,3,1,4],[7,5,6,8]])

1. Find the unique elements in this array:

myArray_1 = np.array([[2,2,3,4],[3,2,3,4]])


2. Sort the elements in this array by its rows:

myArray_2 = np.array([[2,3,1,4],[7,5,6,8]])

Video Guides