2016-05-17 69 views
0

我有些dataframes定义,他们有数据(浮点),他们的名字是:迭代在数据帧的名字与大熊猫名单

df_names = ['df_correl_USDCOP','df_correl_USDCLP','df_correl_USDRUB'] 

我也已经定义了下面列出了能够使用绘制循环:

figures = ['fig'+str(i+1) for i in range(len(df_names))] 
axes = ['ax'+str(i+1) for i in range(len(df_names))] 

for fig, ax, name in zip(figures,axes,df_names): 
    fig,ax = plt.subplots(figsize=(14,10)) 
    ax.plot(name[:],lw=4) 

当这样做时,会出现一个错误,因为在循环中数据框不会被识别,但只是作为一个字符串。

ValueError: could not convert string to float: df_correl_USDCOP 

我在检查这个答案How can I iterate through multiple dataframes to select a column in each in python?,但无法将其应用于此示例。

问题不在于绘图本身,而是如何遍历数据帧名称列表并获取“实际”数据框,而不是字符串的名称。

回答

0

将实际的数据框,而不是字符串与他们的名字放入第二个列表。

如果代码的其余部分是OK(我不能没有数据测试),如果你使用它应该工作而不是执行以下操作:

df_names = ['df_correl_USDCOP','df_correl_USDCLP','df_correl_USDRUB'] 
df_list = [df_correl_USDCOP,df_correl_USDCLP,df_correl_USDRUB] 

figures = ['fig'+str(i+1) for i in range(len(df_names))] 
axes = ['ax'+str(i+1) for i in range(len(df_names))] 

for fig, ax, name in zip(figures,axes,df_list): 
    fig,ax = plt.subplots(figsize=(14,10)) 
    ax.plot(name[:],lw=4)