I have the following code.
dict_of_df={}
for i in range(100):
get_data = [data for data in cur]
df = pd.DataFrame(get_data)
dict_of_df[f"df_{i}"] = df
This gives me a dictionary of 100 dataframes. However, I need to assign each dataframe to a variable for it to be recognised and imported into another program (Microsoft Power BI). Is there a way I can assign each dataframe in the dictionary to a unique variable? Based on what I've read here How to create a new dataframe with every iteration of for loop in Python , a dictionary is the only way of storing the dataframe from each iteration but I need a way to extract it to my workspace.
CodePudding user response:
Use globals()
to create your variables dynamically. Use with caution, it's not really a good practice but it should work:
for i in range(100):
get_data = [data for data in cur]
df = pd.DataFrame(get_data)
globals()[f"df_{i}"] = df # <- replace your dict by globals()