简体   繁体   English

在 concat for 循环中使用 f-string 调用变量

[英]Calling variables with f-string inside concat for loop

I am trying to combine dataframes using pd.concat .我正在尝试使用pd.concat组合数据帧。

I have 7 models, divided into 2 depending on year (2021 and 2022) so in total I have 14 dataframes each containing 4 columns.我有 7 个模型,根据年份(2021 年和 2022 年)分为 2 个,所以我总共有 14 个数据框,每个数据框包含 4 列。 They can be created by the following command:它们可以通过以下命令创建:


concat_list = ['expert_2021', 'expert_2022', 'forecast_168_2021', 'forecast_168_2022', 
               'forecast_24_2021', 'forecast_24_2022', 'forecast_custom_2021', 'forecast_custom_2022', 
               'forecast_lear_2021', 'forecast_lear_2022', 'forecast_standard_2021',
               'forecast_standard_2022', 'auto_2021', 'auto_2022'] 
n = 14
df_list = [pd.DataFrame({"Price_REG1":[], "Price_REG2":[], "Price_REG3":[], "Price_REG4":[]}) for x in range(n)]

for i, j in zip(concat_list, range(14)):
    locals()[i] = df_list[j]

Now, I want to combine these into 8 new dataframes, each representing 1 year and 1 column so 2 years * 4 columns = 8. I want to do this in a for loop.现在,我想将这些组合成 8 个新数据框,每个数据框代表 1 年和 1 列,所以 2 年 * 4 列 = 8。我想在 for 循环中执行此操作。 I am using f-strings to loop over the years and the columns to place the dataframes inside a list.我正在使用 f-strings 循环多年,并使用列将数据帧放在列表中。

year_list = [2021, 2022]
prediction = []
for p in year_list:
    for j, s in zip(range(1,5), range(4)):
        a = pd.concat([f'forecast_24_{p}.Price_REG{j}', f'forecast_168_{p}.Price_REG{j}', 
                       f'forecast_standard_{p}.Price_REG{j}', f'forecast_custom_{p}.Price_REG{j}', 
                       f'expert_{p}.Price_REG{j}', f'forecast_lear_{p}.Price_REG{j}', 
                       f'auto_{p}.Price_REG{j}'], axis=1)
        prediction.append(a)

This gives me TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid这给了我TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

So, I understand that the strings is the problem.所以,我知道字符串是问题所在。 But my question is if there is a way to make these strings to call the dataframes, or if there is some other alternative solution to this kind of problem?但我的问题是是否有办法让这些字符串调用数据帧,或者是否有其他替代解决方案来解决此类问题?

Thank you.谢谢。

You have to use globals() to get your dataframes:您必须使用globals()来获取数据帧:

year_list = [2021, 2022]
prediction = []
for p in year_list:
    for j, s in zip(range(1,5), range(4)):
        df_list2 = [globals()[f'{prefix}_{p}'][f'Price_REG{j}']
                        for prefix in ['forecast_24', 'forecast_168', 'forecast_standard', 
                                       'forecast_custom', 'expert', 'forecast_lear', 'auto']]
                
        a = pd.concat(df_list2, axis=1)
        prediction.append(a)

Note : you can't use locals() here because the scope is the list comprehension and you won't be able to access your dataframes.注意:您不能在此处使用locals() ,因为 scope 是列表理解,您将无法访问您的数据框。

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

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