Python Forum
.format function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: .format function (/thread-36614.html)



.format function - Menthix - Mar-10-2022

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.


RE: .format function - deanhystad - Mar-10-2022

tables = {}
for year in [2016, 2017,2018]:
    tables[year] = pd.read_pickle("./0_DATA/0_raw/Table_PM_{}.pkl".format(year)



RE: .format function - Menthix - Mar-10-2022

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


RE: .format function - deanhystad - Mar-10-2022

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]]



RE: .format function - Menthix - Mar-10-2022

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")


RE: .format function - deanhystad - Mar-11-2022

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.


RE: .format function - stevendaprano - Mar-12-2022

(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.