-1

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.

3
  • a simple search turns this up: stackoverflow.com/questions/6181935/… in short; it's not a good idea Commented Feb 21, 2022 at 14:31
  • Dicts is how you properly handle "variable variables" in Python. I don't know anything about Power BI, but it's weird to me that it forces you to create hundreds of names in your program. Are you sure that's the only way? Commented Feb 21, 2022 at 14:33
  • @timgeb Well PowerBI only supports importing dataframes into a dataset unfortunately so this is my only option, no matter how badly optimised it will be. Is there a way to do it then? Commented Feb 21, 2022 at 14:42

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Corralien, why using globals() may not be a good practice ?
@Timeless. It's not really a bad practice if you control your code and not sharing to other :-). You can create bad named variable like globals()['***'] = 123. Now you have a variable named *** which is unusable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.