Share on:

December 29, 2019

1. Introduction
2. NumPy Arrays
2.1 What’s A NumPy Array
2.2 Creating NumPy Arrays
2.3 Indexing And Modifying 1-D Arrays
2.4 Indexing And Modifying Multidimensional Arrays
2.5 Basic Math
3. Intermediate Array Stuff
3.2 newaxis
3.3 reshape
3.4 boolean indexing
3.5 nan
3.6 infinity
3.7 random
4. Common Operations
4.1 where
4.2 Math Funcs
4.3 all and any
4.4 concatenate
4.5 Stacking
4.6 Sorting
4.7 unique
5. Challenges

This video covers the all() and any() functions which are especially helpful for boolean indexing and excluding nans from data.

# Code

import numpy as np

foo = np.array([
[np.nan, 4.4],
[1.0, 3.2],
[np.nan, np.nan],
[0.1, np.nan]
])

# which rows have at least one nan value
mask = np.any(np.isnan(foo), axis = 1)

# which rows have all nan values
mask = np.all(np.isnan(foo), axis = 1)
foo[mask]

# Transcript

You can use the all() and any() functions to identify arrays where, you guessed it, all or any of the elements match some condition. These are commonly used to identify which rows in a 2d array contain at least one nan value or all nan values. Let’s give it a go.
Here we have a 2d array, foo, with some nan values scattered about.
If we wanted to see which rows have at least one nan value, we can use np.any() with axis = 1. This returns a boolean array that we can use to subset foo.
Similarly, if we wanted to see which rows have all nan values, we can use np.all() with axis = 1.