Error: The truth value of a series is ambiguous. Python & Pandas

心已入冬 提交于 2019-12-02 20:56:07

问题


I'm trying to identify all the options contracts for MSFT and GOOG that have over 10,000 in volume for the day and to print out the name of the symbol.I am getting the error "The truth value of a series is ambiguous.Use a.empty, a.bool(), a.item(), a.any() or a.all()." The error is on line 13. Any help is greatly appreciated.

from pandas_datareader.data import Options
import pandas as pd
from pandas import DataFrame
import datetime

tickers = ['GOOG','MSFT']


for i in tickers:
    option = Options(i,'yahoo')
    data = option.get_all_data()

    if data.Vol > 10000:
         print data.Symbol

    else:
        pass

回答1:


The problem is that the condition (data.Vol > 10000) returns an array of boolean values. NumPy emits that error because it can't know whether you mean to ask "are any of these values > x?", "are all of these values > x?", etc.

In this case you should use logical indexing to get the rows you're interested in: data[data.Vol > 10000].

From there, you can get all the relevant symbols: data[data.Vol > 10000].index.get_level_values('Symbol')



来源:https://stackoverflow.com/questions/36188704/error-the-truth-value-of-a-series-is-ambiguous-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!