Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.format function
#7
(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.
Reply


Messages In This Thread
.format function - by Menthix - Mar-10-2022, 03:56 PM
RE: .format function - by deanhystad - Mar-10-2022, 04:26 PM
RE: .format function - by Menthix - Mar-10-2022, 04:36 PM
RE: .format function - by deanhystad - Mar-10-2022, 04:53 PM
RE: .format function - by Menthix - Mar-10-2022, 05:03 PM
RE: .format function - by stevendaprano - Mar-12-2022, 10:32 AM
RE: .format function - by deanhystad - Mar-11-2022, 05:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  use of format function barryjo 3 1,817 Feb-01-2022, 08:07 AM
Last Post: menator01
  Date format and past date check function Turtle 5 4,718 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Money conversion - problems with lists and .format function fatherted99 1 1,902 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  misunderstanding of format in print function Harvey 2 2,296 Oct-29-2019, 12:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020