me@michellealzoladesign.com

Mon - Fri: 8AM - 5PM MDT

Numpy is a Python library and one of its functions is Sorting used for organizing data into a particular order. Numpy sorting is useful for managing large amounts of data. It is also a necessary step in calculations and analyses.

To use this feature, you need to import the Numpy library. Then use it to sort an array as shown below:

import numpy as np

data = np.array([12, 2, 8, 5, 1, 3])

# Ascending
sorted_data = np.sort(data)
print(f'Ascending: {sorted_data}')
# Output --> Ascending: [ 1  2  3  5  8 12]

# Descending
sorted_data = np.sort(data)[::-1]
print(f'Descending: {sorted_data}')
# Output --> Descending: [12  8  5  3  2  1]

Below are some of the Numpy Sorting exercises I did.

You may see my code in my GitHub repo here.

Recommended Articles