我用pandas创建一个if语句并返回错误,如下所示:
ValueError:系列的真值是不明确的 . 使用a.empty,a.bool(),a.item(),a.any()或a.all() .
我的代码如下:
>>> df_1
timestamp open high low close adjusted_close volume \
0 2017-09-01 1.17 1.24 1.16 1.2 1.2 47932
dividend_amount split_coefficient
0 0.0 1.0
>>> df_o
timestamp open high low close adjusted_close volume \
0 2017-08-31 1.15 1.27 1.06 1.29 1.29 97932
dividend_amount split_coefficient
0 0.0 1.0
>>>if df_1['timestamp']!= df_o['timestamp'].tail(1):
.... print "different date"
>>>>Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/vinus/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 892, 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().
如何避免错误?这是由 if df_1['timestamp']!= df_o['timestamp'].tail(1):
造成的
1 回答
如果总是将一行
DataFrames
转换为标量,然后比较:Sample :
但是如果需要比较
Series
(列)和标量得到另一个True, False
系列,并且不能仅使用带标量的if
:并且对于标量使用2种方法 - any用于检查至少一个
True
和all以检查是否所有值都是True
: