這裏是你如何重現此錯誤:
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
?
- 當數組中的任何值爲True時,是否要將列設置爲
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
「'如果(cleanedData [ '卷價格(傳播)']> cleanedData [ '三角洲VWAP'])'你不能做到這一點,比較收益。一組布爾值,而不是布爾值。「什麼?也許如果他在'cleanData ['Delta VWAP']'之後有逗號' –