1
df2 = df.copy()
    df2['avgTemp'] = df2['avgTemp'] * tempchange
    df2['year'] = df2['year'] + 20
    df_final = pd.concat([df,df2])

OUTPUT:
    Country avgTemp year
0   Afghanistan 14.481583   2012
0   Afghanistan 15.502164   2032
1   Africa  24.725917   2012
1   Africa  26.468460   2032
2   Albania 13.768250   2012
... ... ... ...
240 Zambia  21.697750   2012
241 Zimbabwe    23.038036   2032
241 Zimbabwe    21.521333   2012
242 Åland   6.063917    2012
242 Åland   6.491267    2032

So currently I'm trying to make a loop so I can then do the same calculations for "df_2" and return "df_3", and keep doing this until I have a certain amount of new dataframes that I can then concatinate together. Thank you for your help! :)

So end result should be like df_1, df_2, df_3 and so on. So I can then concat them together into one big dataset

Timo
  • 23
  • 4

1 Answers1

1

Yes, I would use a loop to solve the issue. The x I'm passing in the function range represents the number of loops you wish to do:

lists_of_dfs = []
for i in range(x):
    df_aux = df.copy()
    df_aux['avgTemp'] = df['avgTemp'] * (tempchange ** i)
    df_aux['year'] = df['year'] + (20 * i)
    lists_of_dfs.append(df_aux)

Finally with the full the list of dataframes:

final_df = pd.concat(lists_of_dfs)

The only condition is that the variable tempchange has to be (1+%), it can't be only the % change, otherwise the formula will fail.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53