我理解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()
答案 0 :(得分:16)