我需要找到coulmn两个值在1.5和3.5之间的所有行.我期待的结果是索引1和2的行.我尝试了以下代码,但收到错误.
>>> d = {'one' : [1., 2., 3., 4.],
... 'two' : [4., 3., 2., 1.],
... 'three':['a','b','c','d']}
>>> d
{'three': ['a', 'b', 'c', 'd'], 'two': [4.0, 3.0, 2.0, 1.0], 'one': [1.0, 2.0, 3.0, 4.0]}
>>> DataFrame(d)
one three two
0 1 a 4
1 2 b 3
2 3 c 2
3 4 d 1
>>> df = DataFrame(d)
>>> df[1.5 <= df['two'] <= 3.5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>>
解决方法:
不幸的是,你不能用numpy(因此还有pandas)进行链式比较.改为:
df[(1.5 <= df.two) & (df.two <= 3.5)]