请考虑以下代码段:
>>> 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
到目前为止,这没关系。但如果我想做以下事情,它会给我一个错误:
>>> 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().
似乎在DataFrame中进行矢量化操作时,我们不能在一个赋值中执行两个操作。我想知道有没有办法实现这一目标 1.手动迭代整个数据帧 2.创建两个额外的列来存储中间值