gpt4 book ai didi

python - 为什么 pandas 使用 (&, |) 而不是正常的 pythonic (and, or)?

转载 作者:太空狗 更新时间:2023-10-29 20:42:21 25 4
gpt4 key购买 nike

我了解 pandas docs解释这是惯例,但我想知道为什么?

例如:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6,4), index=list('abcdef'), columns=list('ABCD'))
print(df[(df.A < .5) | (df.B > .5)])
print(df[(df.A < .5) or (df.B > .5)])

返回以下内容:

          A         B         C         D
a -0.284669 -0.277413 2.524311 -1.386008
b -0.190307 0.325620 -0.367727 0.347600
c -0.763290 -0.108921 -0.467167 1.387327
d -0.241815 -0.869941 -0.756848 -0.335477
e -1.583691 -0.236361 -1.007421 0.298171
f -3.173293 0.521770 -0.326190 1.604712
Traceback (most recent call last):
File "C:\test.py", line 64, in <module>
print(df[(df.A < .5) or (df.B > .5)])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

最佳答案

因为 &| 是可覆盖的(可定制的)。您可以为任何编写驱动运算符的代码。

另一方面,逻辑运算符 andor 具有无法修改的标准行为。

参见 here获取相关文档。

关于python - 为什么 pandas 使用 (&, |) 而不是正常的 pythonic (and, or)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20691508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com