简体   繁体   English

在 For 循环中调用不同的 DataFrame

[英]Calling different DataFrames in a For Loop

I am trying to use a for loop where a different DataFrame should be used in each iteration.我正在尝试使用 for 循环,其中每次迭代都应使用不同的 DataFrame。 It is the f'forecast_{s} below which is the problem.下面的f'forecast_{s}就是问题所在。

What I want is that first, the DataFrame forecast_24 should be used, then forecast_168 etc. I can't understand why this is not working.我想要的是,首先应该使用 DataFrame forecast_24 ,然后是forecast_168等。我不明白为什么这不起作用。 Does it have to do with that a string can't call the DataFrame?和字符串不能调用DataFrame有关系吗?

from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error

naive_list = list(['24', '168', 'standard', 'custom'])

  
error = np.zeros((1,8))
for column, i, p, s in zip(df, range(1,5), range(8), naive_list):
    rmse = mean_squared_error(df[column].iloc[500:], f'forecast_{s}'[column].iloc[500:], squared=False)
    mae = mean_absolute_error(df[column].iloc[500:], f'forecast_{s}'[column].iloc[500:])
    error[p,2*p:2*p+2] = [rmse, mae]

TypeError: string indices must be integers

If I understand this correctly, you are trying to access the value forecast_24[column] at the first iteration of the loop and so on.如果我理解正确,您将尝试在循环的第一次迭代中访问值 forecast_24[column] ,依此类推。 Could you maybe do this instead:你能不能改为这样做:

naive_list = list([forecast_24, forecast_168, forecast_standard, forecast_custom])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM