【问题标题】:How to run a for loop through two different variable types in Python?如何在 Python 中通过两种不同的变量类型运行 for 循环?
【发布时间】:2021-05-04 19:03:19
【问题描述】:

我想循环遍历轴(0 到 5 和 0 到 1),以及循环遍历我的系列表 (Pandas) 的列。

我的想法是按照以下思路做一些事情:

for i in range(3):
        for j in range(2):
            sm.graphics.tsa.plot_acf(series['GBP'], ax[i,j], lags=15, zero=False)

但是,这使我无法遍历系列表。这就是我目前的做法:

    sm.graphics.tsa.plot_acf(series['GBP'], ax[0,0], lags=15, zero=False)
    sm.graphics.tsa.plot_acf(series['JPY'], ax[0,1], lags=15, zero=False)
    sm.graphics.tsa.plot_acf(series['DEM'], ax[1,0], lags=15, zero=False)
    sm.graphics.tsa.plot_acf(series['BEF'], ax[1,1], lags=15, zero=False)
    sm.graphics.tsa.plot_acf(series['FRF'], ax[2,0], lags=15, zero=False)
    sm.graphics.tsa.plot_acf(series['ITL'], ax[2,1], lags=15, zero=False)
    sm.graphics.tsa.plot_pacf(series['GBP'], ax[3,0], lags=15, zero=False)
    sm.graphics.tsa.plot_pacf(series['JPY'], ax[3,1], lags=15, zero=False)
    sm.graphics.tsa.plot_pacf(series['DEM'], ax[4,0], lags=15, zero=False)
    sm.graphics.tsa.plot_pacf(series['BEF'], ax[4,1], lags=15, zero=False)
    sm.graphics.tsa.plot_pacf(series['FRF'], ax[5,0], lags=15, zero=False)
    sm.graphics.tsa.plot_pacf(series['ITL'], ax[5,1], lags=15, zero=False)

应该有一个更有效的方法,我只是无法理解它。提前致谢!

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    你可以zip数据框的列和展平的轴一起,然后运行一个循环:

    for axis, col in zip(ax.flatten(), series):
        sm.graphics.tsa.plot_pacf(series[col], ax=axis, lags=15, zero=False)
    

    【讨论】:

    • 正是我想要的!非常感谢。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2018-03-19
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多