Home > Net >  Creating multiple dataframe from dictinary in Python
Creating multiple dataframe from dictinary in Python

Time:10-21

I am looking for a solution to create multiple dataframes from a dictionary that has key value, contains dataframe, such as ;

dict_ = {'df1' : [dataframe],
         'df2' : [dataframe],
              ...           
         'dfi' : [dataframe]}

I've tried with using .get() as following ;

df1,df2,df3,df4 = dict_.get('df1'),dict_.get('df2'),dict_.get('df3'),dict_.get('df4')

But, It does not seem like efficient way to creating multiple dataframe, if dict_ contains huge sets of dataframe.

How can I create multiple dataframe from the dictionary, as well as, creating multiple dataframe as df1, df2, df3, ... dfn depend on the key values from the dictionary that contains dataframe?

CodePudding user response:

Not recommend but work for your situation

variables = locals()
for x, y in dict_.items():
    variables["{0}".format(x)] = y[0]

CodePudding user response:

This should do the trick:

for name, df in dict_.items():
    globals()[name] = df

Alternatively, depending on the scope you need you can use locals:

for name,df in dict_.items():
    locals()[name] = df
  • Related