2016-04-17 54 views
1

進口模塊後加入一個數據幀:無法甚至以下的例子

import Quandl 
import pandas as pd 
from pandas.tools.plotting import df_unique 

讀取API密鑰:

api_key = open('quandlapikey.txt','r').read() 

目前該函數讀取一個CSV文件,以獲取代碼,但是我打算改變這對於s​​qllite ..

def stock_list(): 
    #stocks = pd.read_csv('TID.csv'.rstrip()) 
    stocks = open('TID.csv').readlines() 

    return stocks[0:] 

獲取股票代碼quandl這工程治療。

def getStockValues():  
    stocks = stock_list() 
    main_df = pd.DataFrame() 

    for abbrv in stocks: 

     query = "LSE/" + str(abbrv).strip() 

     df = Quandl.get(query, authtoken=api_key,start_date='2016-04-05', end_date='2016-04-10') 
     df = df['Price'] 
     df.columns = [abbrv] 
     print(query) 
     print(df) 

此聲明由於某種原因導致問題,但循環時無法加入其他股票價格。

 #This statement Prints as 
     print(df.tail(5)) 
     #causes error    
     if main_df.empty: 
      main_df = df 
     else: 
      main_df = main_df.join(df) 
#    exit 

    print('Task done!') 


getStockValues() 

這是打印語句和連接錯誤的輸出。

Result: 

LSE/VOD 
Date 
2016-04-14 226.80 
2016-04-15 229.75 

<ETC for all stocks> 

Traceback (most recent call last): 
    File "H:\Workarea\DataB\SkyDriveP\OneDrive\PyProjects\Learning\21 myPprojects\stockPrices.py", line 49, in <module> 
    getStockValues() 
    File "H:\Workarea\DataB\SkyDriveP\OneDrive\PyProjects\Learning\21 myPprojects\stockPrices.py", line 43, in getStockValues 
    main_df = main_df.join(df) 
    File "H:\APPS\Python35-32\lib\site-packages\pandas\core\generic.py", line 2669, in __getattr__ 
    return object.__getattribute__(self, name) 
AttributeError: 'Series' object has no attribute 'join' 

進一步的測試表明,這個問題似乎是與大熊貓數據的範圍對象這將導致和問題:

main_df = pd.DataFrame() 

for abbrv in stocks:   
    query = "LSE/" + str(abbrv).strip()  
    df = Quandl.get(query, authtoken=api_key,start_date='2016-03-05', end_date='2016-04-10') 
    df = df['Price'] 
    df.columns = [abbrv] 
    #causes error    
    if main_df.empty: 
     main_df = df 
    else: 
     main_df = main_df.join(df) 

但是這並不會導致錯誤但只返回一個數據集:

for abbrv in stocks: 
    main_df = pd.DataFrame() 
    query = "LSE/" + str(abbrv).strip()  
    df = Quandl.get(query, authtoken=api_key,start_date='2016-03-05', end_date='2016-04-10') 
    df = df['Price'] 
    df.columns = [abbrv] 

    if main_df.empty: 
     main_df = df 
    else: 
     main_df = main_df.join(df) 
+1

請提供一個可重現的例子。此外,錯誤消息表明'main_df'是一個Series,而不是一個DataFrame – joris

+0

上面的代碼應該像在導致錯誤一樣工作。然而,在您的評論後,我更改了代碼,這似乎是範圍問題。 –

+0

對不起,這沒有奏效,我現在正在嘗試另一種方式來實現同樣的感謝。 –

回答

0

在我看來,你的代碼的問題是什麼地方在這裏:

... 
df = df['Price'] ## <- you are turning the DataFrame to a Series here 
df.columns = [abbrv] ## <- no effect whatsoever on a Series 
print(query) 
print(df) 

我會做的只是將新行添加到現有的DataFrame中。

## if main_df.empty: ## <- remove this line 
## main_df = df ## This should be changed to the line below 
main_df[abbrv] = df ## This will just add the new column to you df and use the Series as data 
## else: ## <- remove this line 
## main_df = main_df.join(df) ## <- remove this line 
+0

非常感謝,今晚我會試試這個。 –

+0

@teshHi當然,我希望它有幫助。請記住,如果您覺得有用,請「接受」答案。 – Thanos

+0

同樣的問題它只保存一個股票數據框。我仍在學習,所以我會繼續學習,然後重新訪問。非常感謝你。 –