Python NumPy For Your Grandma | Section 4.3 | all() and any()
- 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
- Intermediate Array Stuff
3.4 boolean indexing
- Common Operations
4.2 Math Funcs
4.3 all and any
This video covers the all() and any() functions which are especially helpful for boolean indexing and excluding nans from data.
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) foo[mask] # which rows have all nan values mask = np.all(np.isnan(foo), axis = 1) foo[mask]
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.