Relational Operators with Pandas Series
Let's find out why the puzzle gives a ValueError and how we can fix it.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to see the result.
Python 3.5
import pandas as pddef relu(n):if n < 0:return 0return narr = pd.Series([-1, 0, 1])print(relu(arr))
Explanation
The problematic line is if n < 0:
. Here n
is the result of arr < 0
, which is a pandas.Series
.
In [1]: import pandas as pd
In [2]: arr = pd.Series([-1, 0, 1])
In [3]: arr < 0
Out[3]:
0 True
1 False
2 False
dtype: bool
Once arr < 0
is computed, we use it in an if
statement because of how Boolean values work in Python.
Every Python object, not just True
and False
, has a Boolean value.
We can test the truth value of a Python object using the built-in bool
function. In Python, everything is True
except the following: