如果s是pandas.Series
,我知道我可以这样做:
b = s < 4
要么
b = s > 0
但我不能这样做
b = 0 < s < 4
要么
b = (0 < s) and (s < 4)
什么是基于其他布尔系列的逻辑AND / OR / NOT创建布尔系列的惯用pandas方法?
解决方法:
发现它……&运算符工作,但您需要使用括号来获得正确的优先级并避免错误:
>>> import pandas as pd
>>> s1 = pd.Series([0,1,2,3,4,5,6,0,1,2,3,4])
>>> (s1 < 4) & (s1 > 0)
0 False
1 True
2 True
3 True
4 False
5 False
6 False
7 False
8 True
9 True
10 True
11 False
dtype: bool
>>> s1 < 4 & s1 > 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\app\python\anaconda\1.6.0\lib\site-packages\pandas\core\generic.py",
line 698, 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().
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。