Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.format function
#1
Hello,

I'm trying to create 3 different tables on this model :

Table_2016=pd.read_pickle("./0_DATA/0_raw/Table_PM_2016.pkl")
Table_2017=pd.read_pickle("./0_DATA/0_raw/Table_PM_2017.pkl")
Table_2018=pd.read_pickle("./0_DATA/0_raw/Table_PM_2018.pkl")
But i'd like to do it with a for loop and a .format function, such as :

for year in [2016, 2017,2018]:
    Table_{}.format(year) = pd.read_pickle("./0_DATA/0_raw/Table_PM_{}.pkl".format(year))
The problem is i don't know how to create 3 different names for Table_2016, Table_2017 and Table_2018 with the for loop.

In other words, I'd like this part of the code Table_{}.format(annee) to work...

Thank you very much by advance for your help.
Yoriz write Mar-10-2022, 04:59 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
tables = {}
for year in [2016, 2017,2018]:
    tables[year] = pd.read_pickle("./0_DATA/0_raw/Table_PM_{}.pkl".format(year)
Reply
#3
I don't get it, how do I refer to the new tables. What are their names ? tables2016, tables2017, tables2018 ?
In that case, it doesn't seem to work
Reply
#4
tables[2016], tables[2017], tables[2018]

If you have related values it makes more sense to keep the values in a collection instead of a bunch of variables. In my example the values are kept in a dictionary that uses the year as a key. If you don't care about the year you could save the tables in a list.
tables = [pd.read_pickle("./0_DATA/0_raw/Table_PM_{}.pkl".format(year) for year in [2016, 2017,2018]]
Reply
#5
But i want to create 3 different DataFrame, exactly like the example i gave :

Table_2016=pd.read_pickle("./0_DATA/0_raw/Table_PM_2016.pkl")
Table_2017=pd.read_pickle("./0_DATA/0_raw/Table_PM_2017.pkl")
Table_2018=pd.read_pickle("./0_DATA/0_raw/Table_PM_2018.pkl")
Reply
#6
You will have a dictionary with 3 DataFrames. table[2016] will be a DataFrame. table[2017] will be a DataFrame. table[2018] will be a DataFrame. If you saved them in a list table[0] would be a DataFrame, table[1]...

If you think you need a named variable for every value, you really limit what you can do with Python. Having lots of variables makes it difficult to write programs because nothing can be treated generically. By saving the DataFrames in a colleciton you can do operations in a loop.
for df in table.values():
    df.do_some_dataframe_stuff()
If you dynamically made 3 variables (yes it can be done, but it is a BAD, BAD, BAD idea) you could not use them in a loop.
table2016.do_some_dataframe_stuff()
table2017.do_some_dataframe_stuff()
table2018.do_some_dataframe_stuff()
Yuck!
Collections provide new possibilities for making your programs shorter, easier to write and debug. Anything that you can do with a named variable can be done with a collection. table2016 and table[2016] are equal in how they can be used to access the DataFrame value. Collections organize your data and make your programs easier to understand.

And when you think about it, variables are just keys in a dictionary.
Output:
>>> variables = locals() >>> type(variables) <class 'dict'> >>> variables['x'] = 5 >>> x 5
My programs have very few variables that are not collections. Sure I have temporary variables that are used when processing items in a list, but there are very few cases where a variable that holds a single value makes any sense. There are not a lot of singletons in the world.
perfringo likes this post
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of format function barryjo 3 1,682 Feb-01-2022, 08:07 AM
Last Post: menator01
  Date format and past date check function Turtle 5 4,287 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Money conversion - problems with lists and .format function fatherted99 1 1,825 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  misunderstanding of format in print function Harvey 2 2,190 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