本文介绍了值之间的 pandas 系列过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果 s
是 pandas.Series
,我知道我可以做到:
b = s
或
b = s >0
但我做不到
b = 0
或
b = (0
根据其他布尔系列的逻辑 AND/OR/NOT 创建布尔系列的惯用 Pandas 方法是什么?
解决方案
found it... &
运算符有效,但您需要使用括号来获得正确的优先级并避免错误:
If s
is a pandas.Series
, I know I can do this:
b = s < 4
or
b = s > 0
but I can't do
b = 0 < s < 4
or
b = (0 < s) and (s < 4)
What is the idiomatic pandas method for creating a boolean series based on the logical AND / OR / NOT of other boolean series?
解决方案
found it... the &
operator works, but you need to use parentheses to get the precedence right and avoid an error:
>>> 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().
这篇关于值之间的 pandas 系列过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!