Jupyter/pandas - any() command issue

Guys… going through the Data Analysis Course, and I believe there is a mistake in the exercises but wanted to make sure I wasn’t missing something about the any() command.

The challenge is to write a statement that’s True if any element is Zero, but the answer they give will actually return true if any element isn’t 0 I believe. Same problem in the Numpy exercise as well.

Below was my answer after some playing around with any, and I illustrated that the answer provided still shows True even when no element is 0.

So question is, am I missing something, or is the provided solution wrong? Also, I spent some time trying to figure out how I could use X.any() to actually give the right answer, including X.any(where=0) but couldn’t find a way, and all research showed answers for np.any(), such as np.any(X==0) which is basically my answer, but nothing for np.array.any()… even numpys documents refer you to np.any() when trying to research np.array.any(). Is there a way to do it?

I’m probably digging way to much into what is really a non-issue, but once curiousity is peeked, not much I can do.

From what I can understand from the docs about any,
If any value in a list, tuple, dict, etc… are True, then True will be returned. If all items are False then False is returned. If there is a mix, True will be returned.

Exactly, but the requested problem was to return True if any elements are Zero, which seems the opposite of what Any would do… just wondering if there is a way to use np.array.any() to solve this problem without turning to np.any().

import numpy as np

arr = np.array([0, 0, 0])
print(arr)
print(np.any(arr)) # False
print(np.any(arr == 0)) # True

In the first any call, it checks whether any values are “truthy”. None of them are, so False is returned.

In the second any call, it checks whether any of the array’s values are equal to 0. At least one of them is, so True is returned.

import numpy as np

arr = np.array([0, 1, 0])
print(arr)
print(np.any(arr)) # True
print(np.any(arr == 0)) # True

In the first any call, it checks whether any values are “truthy”. One of them is (1), so True is returned.

In the second any call, it checks whether any of the array’s values are equal to 0. At least one of them is, so True is returned.

Thanks… that all makes since… but in the exercises its suggesting to use arr.any()… not np.any(arr)… thats what confused me and I couldn’t find any good documentation on how to use arr.any() other than just if return true if any values are true. Seems just an issue with the exercise guide. Thanks for all the info :slight_smile:

You can do the following:

arr = np.array([0, 0, 0])
print(arr)
print(arr.any()) # False

and

arr = np.array([0, 1, 0])
print(arr)
print(arr.any()) # True

I am not aware of a way to use a conditional with arr like I did in the second example of my original reply though.