Mar-12-2022, 10:32 AM
(Mar-10-2022, 05:03 PM)Menthix Wrote: But i want to create 3 different DataFrame, exactly like the example i gave :
What if it was 3000 variables?
Honestly, the best solution is what deanhystad has suggested: use a dict keyed with the years. That is the most Pythonic solution.
But if you really, really, really insist on your way, you can do this:
for year in [2016, 2017, 2018]: globals()['Table_{}'.format(year)] = pd.read_pickle("./0_DATA/0_raw/Table_PM_{}.pkl".format(year))Note that this only works with global variables. If you move the code inside a function or method, you will still create globals
Table_2016
etc. Changing to use locals()
instead of globals()
will not work.