我试图比较两列,看看一个值是否大于另一个,但我不断得到一个ValueError:
ValueError:系列的真值是不明确的。使用a.empty, a.bool(),a.item(),a.any()或a.all()
这是引发错误的部分:
if (cleanedData['Roll Price (Spread)'] > cleanedData['Delta VWAP']):
cleanedData["Result"] = "Long"
else:
cleanedData["Result"] = "Short"
我该如何解决这个问题?
答案 0 :(得分:3)
以下是重现此错误的方法:
df = pd.DataFrame({'Roll Price': np.random.randint(1, 10, 10),
'Delta VWAP': np.random.randint(1, 10, 10)})
df
Out:
Delta VWAP Roll Price
0 7 6
1 9 1
2 9 4
3 2 4
4 7 8
5 8 4
6 8 6
7 9 3
8 2 5
9 6 8
if df['Roll Price'] > df['Delta VWAP']:
df['Result'] = 'Long'
Traceback (most recent call last):
File "<ipython-input-18-a07b1f06bd42>", line 1, in <module>
if df['Roll Price'] > df['Delta VWAP']:
File "/home/ayhan/anaconda3/lib/python3.5/site-packages/pandas/core/generic.py", line 955, 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().
错误源于此比较:df['Roll Price'] > df['Delta VWAP']
如果执行此
df['Roll Price'] > df['Delta VWAP']
Out:
0 False
1 False
2 False
3 True
4 True
5 False
6 False
7 False
8 True
9 True
dtype: bool
您会看到结果不是单个True
或False
值,而是一组布尔值。而模棱两可的部分是
Long
?Long
?事实证明答案都不是。您希望进行逐元素比较,并在条件满足时将相应值设置为Long
,否则设置为Short
。
为此,您可以使用np.where
:
cond = df['Roll Price'] > df['Delta VWAP']
df['Result'] = np.where(cond, 'Long', 'Short')
df
Out:
Delta VWAP Roll Price Result
0 7 6 Short
1 9 1 Short
2 9 4 Short
3 2 4 Long
4 7 8 Long
5 8 4 Short
6 8 6 Short
7 9 3 Short
8 2 5 Long
9 6 8 Long