2016-06-13 131 views
1

我正在尝试使用Yahoo Finance API将数据读入DataFrame。但是,当我从列表中读取符号的值时,它们将以DataTable中的单个列结尾。我正在使用API​​,因为我实际上需要诸如股息,P/E和P/E这样的数据,我认为您不能通过数据收集器访问这些数据。我有两个问题:熊猫DataFrame和雅虎财经API

  1. 我如何从一个列表值映射到列在数据帧 (而不是行)
  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,因为形状不匹配,但本质上我想读从列表中的每个值转换成数据表中的列,并最终通过遍历列表o f符号。

感谢所有帮助

+0

为什么你不希望使用'pandas_datareader',其设计是什么? – MaxU

+0

我认为你只能从datareader获得价格和体积数据。我无法找到收集其他统计数据的文档。 – MJMacarty

+0

[这里](http://stackoverflow.com/a/37797875/5741205)是定制统计的一个例子 – MaxU

回答

5

试试这个:

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.)