Search⌘ K

Processing NumPy Arrays

Discover how to process and manipulate NumPy arrays using key functions such as where, mean, variance, and sorting. Learn to save and load arrays, enhancing your data analysis skills with Python's NumPy library for efficient handling of numerical data.

So far, we have discussed the basics of NumPy. Now, these techniques will be applied to data along with some advanced functions to see what more can be achieved.

NumPy array processing functions

Now, let’s discuss some NumPy functions that help in array processing.

NumPy where function

Let’s look at an example of the where function and try to understand it.

Python 3.5
import numpy as np
# Declare 2 arrays
arr1 = np.array([10, 20, 30, 40])
arr2 = np.array([500, 600, 700, 800])
cond = np.array([False, True, False, True]) # Create an array with bool operators
res = np.where(cond, arr1, arr2) # apply the where condition
print(res)

What exactly happened here? The NumPy where function took 3 values cond, arr1, and arr2. This function first checks the value for cond. If the value of cond is True, then ...