我用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):
答案 0 :(得分:0)
如果始终有一行DataFrames
将值转换为标量,然后进行比较:
if df_1['timestamp'].iat[0] != df_o['timestamp'].iat[0]:
if df_1['timestamp'].values[0] != df_o['timestamp'].values[0]:
if df_1['timestamp'].item() != df_o['timestamp'].item():
<强>示例强>:
df_1 = pd.DataFrame({'timestamp':['2017-09-01']})
df_o = pd.DataFrame({'timestamp':['2017-08-31']})
if df_1['timestamp']!= df_o['timestamp'].tail(1):
print ('not equal')
ValueError:系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
print (df_1['timestamp']!= df_o['timestamp'].tail(1))
0 True
Name: timestamp, dtype: bool
print ((df_1['timestamp']!= df_o['timestamp'].tail(1)).values[0])
True
但是,如果需要将Series
(列)与标量进行比较,请获取另一个True, False
系列,并且不能仅使用if
使用标量:
df_1 = pd.DataFrame({'timestamp':['2017-09-01']})
df_o = pd.DataFrame({'timestamp':['2017-08-31', '2017-09-01', '2017-06-10', '2017-08-12']})
print (df_o['timestamp'].tail(3) != df_1['timestamp'].iat[0])
1 False
2 True
3 True
Name: timestamp, dtype: bool
对于标量,使用2种方法 - any
来检查至少一个True
和all
,以检查所有值是否为True
s:
print ((df_o['timestamp'].tail(3) != df_1['timestamp'].iat[0]).any())
True
print ((df_o['timestamp'].tail(3) != df_1['timestamp'].iat[0]).all())
False