【问题标题】:pandas DataFrame and Yahoo Finance APIpandas DataFrame 和 Yahoo Finance API
【发布时间】:2023-03-14 09:50:01
【问题描述】:

我正在尝试使用 Yahoo Finance API 将数据读入 DataFrame。但是,当我从列表中读取符号的值时,它们最终会出现在 DataTable 的单个列中。我使用 API 是因为我实际上想要诸如股息、市盈率之类的数据,而且我认为您无法使用 datareader 访问这些数据。我有两个问题:

  1. 如何从列表中获取值以映射到 DataFrame 中的列 (而不是行)
  2. 我将如何完成我想要为股票代码列表做的事情

    import urllib2
    from pandas import DataFrame
    def get_data2(symbol):
        columns = ['last','date','change','high','low','vol']    
        url = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1c1hgv" % symbol
        file =urllib2.urlopen(url)    
        s = file.read()
        file.close()
        s= s.strip()
        L = s.split(',')
        L[0] = L[0].replace('"','')
        L[2] = L[2].replace('"','')
        D = DataFrame(L, columns=columns)
        return D
    

使用此代码,我得到一个 ValueError,因为形状不匹配,但本质上我想将列表中的每个值读入 DataTable 中的一列,并最终遍历符号列表。

感谢您的帮助

【问题讨论】:

  • 为什么不想使用专为此设计的pandas_datareader
  • 我认为您只能从 datareader 获取价格和数量数据。我找不到用于收集其他统计信息的文档。
  • here 是自定义统计的示例
  • 很棒的麦克斯,谢谢。我只是想知道哪里有一些全面的文档。

标签: python-2.7 api pandas dataframe yahoo-finance


【解决方案1】:

试试这个:

In [23]: from pandas_datareader import data

In [24]: data.DataReader('GOOG', 'yahoo', '2016-06-01', '2016-06-13')
Out[24]:
                  Open        High         Low       Close   Volume   Adj Close
Date
2016-06-01  734.530029  737.210022  730.659973  734.150024  1250800  734.150024
2016-06-02  732.500000  733.020020  724.169983  730.400024  1337600  730.400024
2016-06-03  729.270020  729.489990  720.559998  722.340027  1222700  722.340027
2016-06-06  724.909973  724.909973  714.609985  716.549988  1565300  716.549988
2016-06-07  719.840027  721.979980  716.549988  716.650024  1336200  716.650024
2016-06-08  723.960022  728.570007  720.580017  728.280029  1582100  728.280029
2016-06-09  722.869995  729.539978  722.335999  728.580017   985900  728.580017
2016-06-10  719.469971  725.890015  716.429993  719.409973  1206000  719.409973

Demo for building pandas Panel when pulling data for multiple tickers

Demo for pulling custom Yahoo quotes (for example: Market Cap, Div Yield, EPS Est Next Quarter, etc.)

【讨论】:

    猜你喜欢
    • 2018-08-11
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多