使用pandas在数据帧名称列表中迭代

2024-04-25 16:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我定义了一些数据帧,它们有数据(浮点数),它们的名称是:

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

我还定义了以下列表,以便能够使用循环绘制:

^{pr2}$

执行此操作时,会出现一个错误,即循环中的dataframe未被识别为,而只是一个字符串。在

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?,但无法将其应用到这个示例中。在

问题不在于绘图本身,而是如何通过dataframe名称列表进行迭代,得到“实际的”dataframe,而不是一个带有其名称的字符串。在


Tags: to数据字符串in名称dataframedf列表
1条回答
网友
1楼 · 发布于 2024-04-25 16:41:15

将实际的数据帧而不是字符串及其名称放入第二个列表中。在

如果代码的其余部分正常(如果没有数据,我无法测试),那么使用以下代码应该可以工作:

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)

相关问题 更多 >

    热门问题