1

I have a dictionary with many keys where one key matches to a list of values, like so:

data['0'] = [A,B,C,D,E,F]
data['0.044'] = [G,H,I,J,K,L]
data['0.047'] = [M,N,O,P,Q,R]

I want to convert all these key-value pairs into pandas dataframes in a loop, and save all the dataframes in the dictionary, such that by accessing the same key above I can see the associated dataframe.

Desired:

In[1] data['0'] Out[1]: col 0 A 1 B 3 C 4 D 5 E 6 F

I have tried fiddling around with loops a bunch, even tried using a list instead of dicts, but to not much avail. Any ideas?

guru
  • 173
  • 13
  • 2
    Please provide a sample dictionary and desired DataFrame(s). Please read [how to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your post correspondingly. – MaxU - stand with Ukraine Sep 16 '17 at 20:20

1 Answers1

0
In [207]: d = {
     ...:   '0': ['A','B','C','D','E','F'],
     ...:   '0.044': ['G','H','I','J','K','L']
     ...: }
     ...:

In [208]: d
Out[208]: {'0': ['A', 'B', 'C', 'D', 'E', 'F'], '0.044': ['G', 'H', 'I', 'J', 'K', 'L']}

In [209]: dfs = {k:pd.DataFrame(v, columns=['val']) for k,v in d.items()}

In [210]: dfs['0']
Out[210]:
  val
0   A
1   B
2   C
3   D
4   E
5   F

In [211]: dfs['0.044']
Out[211]:
  val
0   G
1   H
2   I
3   J
4   K
5   L
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419