2017-04-06 64 views
1

我試圖在單個圖中顯示n個圖表,n是美國國家編號的數字。在for循環中的單個圖中的多個圖表

編譯器不喜歡那些2線x[j] = df['Date'] y[j] = df['Value']

=>類型錯誤:「NoneType」對象不與該特定錯誤標化的

import quandl 
import pandas as pd 
import matplotlib.pyplot as plt 

states = pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states') 
j = 0 
x = [] 
y = [] 

for i in states[0][0][1:]: 
     df = quandl.get("FMAC/HPI_"+i, authtoken="yourtoken") 
     df = df.reset_index(inplace=True, drop=False) 
     x[j] = df['Date'] 
     y[j] = df['Value'] 
     j += 1 

plt.plot(x[j],y[j]) 
plt.xlabel('Date') 
plt.ylabel('Value') 
plt.title('House prices') 
plt.legend() 
plt.show() 
+0

首先,你還沒有定義'x'和'y'。所以放在某處'x = []; Y = []'。其次,你需要追加新的項目,因爲在第j步中,x [j]實際上並不存在。使用x.append(...)。關於繪製數據框列表可能還有其他問題,我不確定它是否可行。 – ImportanceOfBeingErnest

+0

感謝您的幫助,去搜索其他東西 – louisdeck

回答

1

你的問題是,要使用的參數inplace並分配回變量df。當使用inplace參數等於True時,返回值爲None。

print(type(df.reset_index(inplace=True, drop=False))) 
NoneType 

print(type(df.reset_index(drop=False))) 
pandas.core.frame.DataFrame 

二者必選其一inplace=True和不分配回DF:

df.reset_index(inplace=True, drop=False) 

或使用默認就地=假,並分配回變量DF

df = df.reset_index(drop=False) 

還有一些其他的邏輯錯誤在這裏。

編輯得到一個工作表(限20個用於測試)

for i in states[0][0][1:20]: 
     df = quandl.get("FMAC/HPI_"+i, authtoken="yourtoken") 
     df.reset_index(inplace=True, drop=False) 
     plt.plot('Date','Value',data=df) 


# plt.plot(x[j],y[j]) 
plt.xlabel('Date') 
plt.ylabel('Value') 
plt.title('House prices') 
plt.show() 

enter image description here

+0

非常感謝您的解釋,非常感謝。 幾天前啓動Python和那些庫,得到改進:D – louisdeck

+0

不客氣。 –