Home > OS >  How to generate multiple dataframes from a dictionary?
How to generate multiple dataframes from a dictionary?

Time:07-27

I have a dictionary like the following

dict = {“df_text":df1, “df_logo":df2, “df_person":df3}

Each of the values in the dictionary is a dataframe. Yet my actual dictionary is larger, so I want to make a loop that generate multiple dataframes from all of the components of this dict. In a way that “key” be the name of the dataframe and the corresponding value the dataframe’s value.

ex. df_text=pd.DataFrame(df1)

How can I do this?

CodePudding user response:

You can add the contents of your dict as variables to vars:

for k, v in dict.items():
    vars()[k] = v

After that you can access them simply as df_text, df_logo etc.

(as you wrote in your question, the values of your dict are already dataframe, so I assume you don't want to wrap them once more into a dataframe)

  • Related