2017-04-06 67 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

不客气。 –