Consider the following code snippet:
>>> import pandas as pd
>>> A = pd.DataFrame([[1,2],[3,4],[8,9]])
>>> A["result"] = A[0] % 2 == 0
>>> A
0 1 result
0 1 2 False
1 3 4 False
2 8 9 True
So far this is ok. But if I want to do the following, it gives me an error:
>>> A["result"] = (A[0] % 2 == 0) and (A[1] % 2 == 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/space/env/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 953, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
It seems that, when it comes to vectorized operation in DataFrame, we can not do two operations in one assignment. I'm wondering is there a way to achieve this without 1. manually iterating over the whole dataframe 2. creating two additional columns to store the intermediate value