Relational Operators with Pandas Series
Explore how to apply relational operators to pandas Series and deal with their unique boolean logic. Learn to avoid common ValueError exceptions by using methods like all() and any(), and understand how to create functions that work on scalars and vectors in pandas and NumPy.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to see the result.
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: