# Python NumPy For Your Grandma - 4.3 all() and any()

Contents

In this video we’ll see how 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 often used to identify which rows in a 2d array contain at least one `nan` value or all `nan` values, so let’s give that a try.

Here we have a 2d array, `foo`, with some `nan` values scattered about.

``````import numpy as np

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

If we wanted to see which rows have at least one `nan` value, we can start by calling `np.isnan()` `foo` which tells us whether each value in `foo` is `nan`.

``````np.isnan(foo)
## array([[ True, False],
##        [False, False],
##        [ True,  True],
##        [False,  True]])
``````

And then we can wrap that inside `np.any()` and set `axis=1`.

``````np.any(np.isnan(foo), axis = 1)
## array([ True, False,  True,  True])
``````

If you assign this to a variable called `mask`, you can use it as a boolean index to return rows of `foo` with at least one `nan` value.

``````mask = np.any(np.isnan(foo), axis = 1)
## array([[nan, 4.4],
##        [nan, nan],
##        [0.1, nan]])
``````

Alternatively, if you wanted to see which rows of `foo` have all `nan` values, you can do the same thing with the `all()` function.

``````mask = np.all(np.isnan(foo), axis = 1)