0

I see a comment below saying do it in containers like list or dictionary. Could you give me an example that will have the same effect like having a variable?

I wrote some pseudo-input code and the final output I wanted to see (Just to have these variables in the system)

  1. input:

     List_Tickers = {AMZN, FB, AAPL}
    
     def dataDataframe(symbols,TradeApp_obj):
     "returns extracted historical data in dataframe format"
     df_data = {}
     for symbol in symbols:
         df_data[symbol] = pd.DataFrame(TradeApp_obj.data[symbols.index(symbol)])
         df_data[symbol].set_index("Date",inplace=True)
     return df_data
    
    
     for ticker in List_Tickers
         df_{}.format(ticker)= pd.DataFrame[ticker]
    
  2. output:

     df_AMZN = pd.DataFrame["AMZN"]
    
     df_FB = pd.DataFrame["FB"]
    
     df_AAPL = pd.DataFrame["AAPL"]
    
  3. extra information

    a. I want to just change the contents within List_Tickers that it just auto-generate these dataframes and variables.

    b. The above function dataDataframe() will generate dataframes for each ticker.

    c. In the end, I want to save these dataframes into excel files individually for its ticker

Ryan C
  • 15
  • 4
  • 4
    Don't. Don't dynamically create variables. Use a *container* like a list or a dict – juanpa.arrivillaga Jul 03 '21 at 04:16
  • I'm wondering if you really need to store these variables in memory. Assuming that in the end you simply need to store it in an excel file, are you sure it's necessary to have them as variables? Why not simply do it generically within a function and save it right there? – Aidos Jul 03 '21 at 05:12

2 Answers2

0

You can do so by using global variables dictionary, execute variable setting code or something like a class. I would recommend class method or you should be better with only the dictionary.

global variable method:

globals()["My_Variable_Name"] = "Hello, World!"
print(My_Variable_Name)

The above code will print Hello, World but the problem is that you will recieve warnings if you are using an IDE.

Now the method using a custom class:

class DataNamespace():
    pass

# Now you can do:
df_data2 = DataNamespace()
for k, v in df_data.items():
    setattr(df_data2, k, v)

Now you should be able to:

df_data2.df_AMZN
df_data2.df_AAPL
# and all the entries in your dictionary

for information about setattr look at this: https://docs.python.org/3/library/functions.html#setattr

Code execution method:

def function(...):
    # Your code that sets df_data2
    for k in df_data2.keys():
        exec(f"{k} = df_data2[k]")

Note: the above is not recommended and using the above might make your code unreadable very fast

AmaanK
  • 1,032
  • 5
  • 25
0

I strongly agree with @juanpa.arrivillaga. While it seems attractive, dynamically generating variables is bad practice and won't give you any advantage. Here is a way to use a dict as container :

List_Tickers = {"AMZN", "FB", "AAPL"}
dfs_container = {key: function_yhat_computes_df(key) for key in List_Tickers}

Then you can access the results by key:

dfs_container["AMZN"]
mozway
  • 194,879
  • 13
  • 39
  • 75