2016-06-13 145 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.)