3 回答
TA贡献1818条经验 获得超7个赞
(a['x']==1) and (a['y']==10)
(a['x']==1)(a['y']==10)
ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().
empty(), all()any()
&
(a['x']==1) & (a['y']==10)
&==a['x']==1 & a['y']==10a['x'] == (1 & a['y']) == 10(a['x'] == (1 & a['y'])) and ((1 & a['y']) == 10)Series and SeriesandValueError
TA贡献2016条经验 获得超9个赞
Pandas中的逻辑运算符是 &, |和 ~,和括号 (...)很重要!
and, ornot
exp1exp2
exp1 and exp2 # Logical ANDexp1 or exp2 # Logical ORnot exp1 # Logical NOT
exp1 & exp2 # Element-wise logical ANDexp1 | exp2 # Element-wise logical OR~exp1 # Element-wise logical NOT
ValueError
(exp1) op (exp2)
(df['col1'] == x) & (df['col2'] == y)
布尔索引&|~
np.random.seed(0)df = pd.DataFrame(np.random.choice(10, (5, 3)), columns=list('ABC'))df
A B C0 5 0 31 3 7 92 3 5 23 4 7 64 8 8 1逻辑与
df
&
另一个常见操作是使用布尔向量过滤数据。营办商包括: |为 or,&为 and,和 ~为 not. 必须使用括号对它们进行分组。,因为默认情况下Python将计算一个表达式,如 df.A > 2 & df.B < 3如 df.A > (2 & df.B) < 3,而所需的评估顺序是 (df.A > 2) & (df.B < 3).
&:
df['A'] < 50 False1 True2 True3 True4 FalseName: A, dtype: bool df['B'] > 50 False1 True2 False3 True4 TrueName: B, dtype: bool
(df['A'] < 5) & (df['B'] > 5)0 False1 True2 False3 True4 Falsedtype: bool
df[(df['A'] < 5) & (df['B'] > 5)] A B C1 3 7 93 4 7 6
<>
df['A'] < 5 & df['B'] > 5
df['A'] < (5 & df['B']) > 5
df['A'] < something_you_dont_want > 5
(df['A'] < something_you_dont_want) and (something_you_dont_want > 5)
# Both operands are Series...something_else_you_dont_want1 and something_else_you_dont_want2
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
避免部分分组
df['A'].lt(5)0 True1 True2 True3 True4 FalseName: A, dtype: bool df['B'].gt(5)0 False1 True2 False3 True4 TrueName: B, dtype: bool
df['A'].lt(5) & df['B'].gt(5)0 False1 True2 False3 True4 Falsedtype: bool
╒════╤════════════╤════════════╕│ │ Operator │ Function │╞════╪═══════════ ═╪════════════╡│ 0 │ > │ gt │├────┼────────────┼──────────── ┤│ 1 │ >= │ ge │├────┼────────────┼────────────┤│ 2 │ < │ lt │├────┼────────────┼────────────┤│ 3 │ <= │ le │├────┼ ────────────┼────────────┤│ 4 │ == │ eq │├────┼────────────┼─ ───────────┤│ 5 │ != │ ne │╘════╧════════════╧════════════╛
DataFrame.queryeval):
df.query('A < 5 and B > 5')
A B C1 3 7 93 4 7 6queryeval
operator.and_Series.__and__
import operator operator.and_(df['A'] < 5, df['B'] > 5)# Same as,# (df['A'] < 5).__and__(df['B'] > 5) 0 False1 True2 False3 True4 Falsedtype: bool df[operator.and_(df['A'] < 5, df['B'] > 5)] A B C1 3 7 93 4 7 6
np.logical_andlogical_and.reduce)np.logical_and
np.logical_and(df['A'] < 5, df['B'] > 5)0 False1 True2 False3 True4 FalseName: A, dtype: bool df[np.logical_and(df['A'] < 5, df['B'] > 5)] A B C1 3 7 93 4 7 6
np.logical_andreducelogical_andm1m2m3&
m1 & m2 & m3
np.logical_and.reduce([m1, m2, m3])
import operator cols = ['A', 'B']ops = [np.less, np.greater]values = [5, 5]m = np.logical_and.reduce([op(df[c], v) for op, c, v in zip(ops, cols, values)])m # array([False, True, False, True, False])df[m] A B C1 3 7 93 4 7 6
逻辑OR
df
|
df['A'] == 30 False1 True2 True3 False4 FalseName: A, dtype: bool df['B'] == 70 False1 True2 False3 True4 FalseName: B, dtype: bool
(df['A'] == 3) | (df['B'] == 7)0 False1 True2 True3 True4 Falsedtype: bool df[(df['A'] == 3) | (df['B'] == 7)] A B C1 3 7 92 3 5 23 4 7 6
df[df['A'].eq(3) | df['B'].eq(7)] A B C1 3 7 92 3 5 23 4 7 6
operator.or_Series.__or__
operator.or_(df['A'] == 3, df['B'] == 7)# Same as,# (df['A'] == 3).__or__(df['B'] == 7)0 False1 True2 True3 True4 Falsedtype: bool df[operator.or_(df['A'] == 3, df['B'] == 7)] A B C1 3 7 92 3 5 23 4 7 6
np.logical_orlogical_or:
np.logical_or(df['A'] == 3, df['B'] == 7)0 False1 True2 True3 True4 FalseName: A, dtype: bool df[np.logical_or(df['A'] == 3, df['B'] == 7)] A B C1 3 7 92 3 5 23 4 7 6
logical_or.reduce:
np.logical_or.reduce([df['A'] == 3, df['B'] == 7])# array([False, True, True, True, False])df[np.logical_or.reduce([df['A'] == 3, df['B'] == 7])] A B C1 3 7 92 3 5 23 4 7 6
逻辑不
mask = pd.Series([True, True, False])
[False, False, True]
~
~mask0 False1 False2 Truedtype: bool
~(df['A'] == 3)0 True1 False2 False3 True4 TrueName: A, dtype: bool
mask.__invert__()0 False1 False2 Truedtype: bool
operator.inv__invert__
operator.inv(mask)0 False1 False2 Truedtype: bool
np.logical_not
np.logical_not(mask)0 False1 False2 Truedtype: bool
np.logical_andnp.bitwise_and, logical_orbitwise_orlogical_notinvert.
添加回答
举报
