3

I have a dictionary d full of a collection of dataframes:

key     type          size       value
gm1     dataframe     mxn        ....
gm2     dataframe     mxN        ...
..
gm10    dataframe     nxM        ...

I want to output these dataframes one-by-one using their keys as the names of the new dataframes. I try doing this first by iterating over the keys of the dictionary:

for key in d.keys():
    key = d[key]

This gives me the output of the last dataframe gm10 and obviously names it key because it simply updates the name as key rather than naming the output dataframes as their keys.

Is there an easy way to get gm1, gm2,....gm10 each as their own named dataframes?

HelloToEarth
  • 2,027
  • 3
  • 22
  • 48
  • You can but not recommended , this will need `locals` check https://stackoverflow.com/questions/54077811/split-a-dataframe-into-multiple-dataframes/54078036#54078036 – BENY Mar 22 '19 at 14:53
  • You can already access the dataframes with `d['gm1']` ... `d['gm10']`, which is way more versatile than finding a dynamic way to refer to them as `gm1` ... `gm10` in your code. Is there a reason why you must have them the latter? It might help answer your question better to understand the background. – r.ook Mar 22 '19 at 15:00

1 Answers1

0

This is the dictionnary

d={}

To get the dataframes' names & print the list of names

listing=list(d)
print(listing)

Loop through the dataframes'names

& get the dataframe one-by-one

& print the dataframe one-by-one

for l in listing:
 df= d[l]
 print(l)
 print (df)
  • 1
    This does not answer the question, this only prints the information but does not allocate the data into a dynamic object – piyuw Aug 01 '21 at 14:43